mPDF 中的自定义字体无法加载

Custom font in mPDF won´t load

我正在使用 7.x 版的 mPDF 并尝试遵循此文档: https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html

我就是无法让它工作。没有错误,但字体仍然是默认的 mPDF 字体。 我还尝试用另一种方式来回答这些问题:

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

adding font to mPDF

但我猜它们不起作用,因为它们可能只适用于比 7.X 更旧的版本...所以这是我最近尝试使用 7.x 文档中的信息的尝试.

这是我的 php 文件:

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

还有我的css:

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

我的自定义字体存储在 "fonts" 中,它与 php 文件位于同一文件夹中。

开始使用了。不确定是什么把戏,但这是工作代码:

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

if (!defined('_MPDF_TTFONTPATH')) {
    // an absolute path is preferred, trailing slash required:
    define('_MPDF_TTFONTPATH', realpath('fonts/'));
    // example using Laravel's resource_path function:
    // define('_MPDF_TTFONTPATH', resource_path('fonts/'));
}

function add_custom_fonts_to_mpdf($mpdf, $fonts_list) {

    $fontdata = [
        'bentonsans' => [
            'R' => 'BentonSans.ttf',
            'B' => 'BentonSans-Bold.ttf',
        ],
    ];

    foreach ($fontdata as $f => $fs) {
        // add to fontdata array
        $mpdf->fontdata[$f] = $fs;

        // add to available fonts array
        foreach (['R', 'B', 'I', 'BI'] as $style) {
            if (isset($fs[$style]) && $fs[$style]) {
                // warning: no suffix for regular style! hours wasted: 2
                $mpdf->available_unifonts[] = $f . trim($style, 'R');
            }
        }

    }

    $mpdf->default_available_fonts = $mpdf->available_unifonts;
}

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/upload']);

add_custom_fonts_to_mpdf($mpdf);

$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');
?>

问题出在 Mpdf 版本 7 中,配置作为单个参数(数组)传递,而您将多个参数传递给构造函数。

这是一个有效的配置:

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

这是 docs 中说明的真正可行的解决方案: "Define the font details in fontdata configuration variable - font name must be lowercase".

所以BentonSans必须改为bentonsans

代码为:

$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',
        'fontDir' => array_merge($fontDirs, [
            __DIR__ . '/fonts'
        ]),
        'fontdata' => $fontData + [
            'bentonsans' => [
                'R' => 'BentonSans.ttf',
                'I' => 'BentonSans-Bold.ttf',
            ],
        ],
        'default_font' => 'bentonsans'
    ]
);