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

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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?