• laravel 使用mpdf将html转化成pdf


    • 安装方式
      composer require mpdf/mpdf

    • 常用配置

    $defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
    // 获取默认的字体包文件路径
    $fontDirs = $defaultConfig['fontDir'];
    
    $defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
    // 获取默认的字体包
    $fontData = $defaultFontConfig['fontdata'];
    $options = [ 
        'debug'=>true,
        'mode' => 'utf-8',
        'format' => 'A4',
        'useAdobeCJK' => true,
        'baseScript'=>1,
        'autoVietnamese'=>true,
        'autoArabic'=>true,
        'autoScriptToLang' => true,
        // 'autoLangToFont'   => true,
        'useSubstitutions' => true,
        'mgl' => 0,
        'mgr' => 0,
        'mgt' => 0,
        'mgb' => 0,
        'mgh' => 0,
        'mgf' => 0,
        'margin_left' => 10,
        'margin_right' => 10,
        'margin_top' => 10,
        'margin_bottom' => 16,
        'orientation' => 'P',
        // 'setAutoTopMargin' => 'stretch', 
        'setAutoBottomMargin' => 'stretch',
        'default_font_size' => 14,
    	'fontDir' => array_merge($fontDirs, [
            public_path('ttf')
        ]),
        'fontdata' => $fontData + 
            [
                'customsongti' => [
                    // 宋体
                    'R' => 'songti.ttf'
                ],
                'customtimes'=>[
                    // 新罗马斜体
                    'R'=>'TimesNewRomanItalic.ttf'
                ]
            ],
        'default_font'=>'customsongti'
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    业务需求中,需要将部分文字和图片隐藏,但是需要保留对应的宽度到pdf上。

    1. 文字隐藏。将所有要隐藏的文字转换为span元素,增加一个类名,新建一个css文件,添加样式visibility: hidden !important;,通过WriteHTML($cssHtml, HTMLParserMode::HEADER_CSS)加载即可生效
    2. 图片隐藏。图片用上述方式一直不生效,后面尝试了直接把样式写到标签的style中生效了,style="visibility: hidden;height:0.1px;"。(这里是期望图片隐藏后空白内容的高度跟左右文字的高度一致,所以加入了height:0.1px,为什么设置0.1呢,因为设置0也不正常(时间太久,忘记是高度又撑起来还是别的问题了))
    3. 页面中的内容包含中文、英文、数字和变量,在数学学科中,变量需要显示为新罗马斜体,其他内容则用宋体,但是mpdf并不支持css中设置多字体的方法。故将所有的斜体的字母匹配出来,单独写样式。
    // 匹配所有的斜体中的字母和数字
     $html = preg_replace_callback('/(.*)<\/span>/isU',function($matches){
     			// 如果有斜体样式
                if(strpos($matches[1],'font-style:italic;') !== false){
                    $text = $matches[2];
                    // 要用新字体的字符
                    $chars = ['a',  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','A','B','C','D','E','F','G'.'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z','+','-','=','0','1','2','3','4','5','6','7','8','9'];
                    // 匹配字符
                    $preg = '/['.implode('',$chars).']+/isu';
                    $text = preg_replace_callback($preg,function($matches){
                            $chars = $matches[0];
                            // 给指定的字符加样式类
                            return ''.$chars.'';
                    },$text);
                    // 原有的样式依旧保留
                    return '.$matches[1].'">'.$text.'';
                }else{
                    return $matches[0];
                }
            }, $html);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    数据结构笔记之连通图与强连通图
    【PHPWrod】使用PHPWord导出word文档
    含文档+PPT+源码等]精品基于Uniapp+SSM实现的酒品移动电商平台app[包运行成功]计算机毕业设计Android项目源码
    什么是绩效管理?
    【C++】string详细介绍及模拟实现string类
    不同网段的IP怎么互通
    多线程-线程与进程、线程的实现方式(第十八天)
    树链剖分入门指南大全
    Bellman_Ford 算法(解决负权回路,边数限制的最短距离)
    神经网络最重要的是什么,什么神经网络最好用
  • 原文地址:https://blog.csdn.net/Golderant/article/details/132902937