gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:04:03 26 4
gpt4 key购买 nike

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

我只是无法让它工作。没有错误,但字体仍然是默认的 mPDF 字体。
我还尝试以另一种方式使用以下答案:

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

但我想它们不起作用,因为它们可能只适用于比 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;
}

我的自定义字体存储在与 php 文件位于同一文件夹中的“字体”中。

最佳答案

这是docs 中指示的真正可行的解决方案:
“在 fontdata 配置变量中定义字体详细信息 - 字体名称必须小写”。

所以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'
]
);

关于php - mPDF 中的自定义字体不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52708998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com