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'
];

业务需求中,需要将部分文字和图片隐藏,但是需要保留对应的宽度到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 style="(.*)">(.*)<\/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 '<span class="mathChars">'.$chars.'</span>';
                 },$text);
                 // 原有的样式依旧保留
                 return '<span style="'.$matches[1].'">'.$text.'</span>';
             }else{
                 return $matches[0];
             }
         }, $html);
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!