dongtan5555 2018-10-08 19:38
浏览 259
已采纳

mPDF中的自定义字体不会加载

I'm using version 7.x of mPDF and tried to follow this documentation: https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html

I just can't get it to work. No errors, but the font is still the default mPDF font. I also tried to do it another way with the answers from these:

How to generate PDF using mPDF and add custom Google font to it?

php mPDF, impossible to set font-family and font-size

adding font to mPDF

But I guess they don´t work, as they might only be for older version than 7.X ...So here's is my latest attempt trying to use the information for the 7.x documentation.

Heres my php file:

require_once __DIR__ . '/vendor/autoload.php';

$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/upload'],
    ['fontdata' => $fontData + [
        'BentonSans' => [
            'R' => 'BentonSans.ttf',
            'I' => 'BentonSans-Bold.ttf',
        ]
    ],
    'default_font' => 'BentonSans'
]);

$url = rawurldecode($_REQUEST['url']);
$html = file_get_contents($url);

$stylesheet = file_get_contents('style.css');

$mpdf->setBasePath($url);
$mpdf->AddFontDirectory('fonts');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output('filename.pdf','I');

And my css:

body {
    font-family: 'BentonSans';
    font-size: 14px; 
    font-style: normal; 
    font-variant: normal; 
    font-weight: normal; 
    line-height: 20px;
}

My custom fonts are stored in "fonts" which is in the same folder as the php file.

  • 写回答

2条回答 默认 最新

  • dpd7195 2018-10-22 23:10
    关注

    The issue is in Mpdf version 7 the configuration is passed as a single parameter (an array), while you were passing in multiple parameters to the constructor.

    This is a valid configuration:

    $mpdf = new \Mpdf\Mpdf(
        [
            'tempDir'      => __DIR__ . '/upload',
            'fontdata'     => $fontData + [
                    'BentonSans' => [
                        'R' => 'BentonSans.ttf',
                        'I' => 'BentonSans-Bold.ttf',
                    ],
            ],
            'default_font' => 'BentonSans',
        ]
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?