我正在使用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
但是我想它们不起作用,因为它们可能仅适用于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文件位于同一文件夹中的“字体”中。
答案 0 :(得分:2)
这是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'
]
);
答案 1 :(得分:0)
开始工作。不知道是什么招数,但这是工作代码:
<?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');
?>
答案 2 :(得分:0)
问题是在Mpdf版本7中,当您将多个参数传递给构造函数时,配置作为单个参数(数组)传递。
这是有效的配置:
$mpdf = new \Mpdf\Mpdf(
[
'tempDir' => __DIR__ . '/upload',
'fontdata' => $fontData + [
'BentonSans' => [
'R' => 'BentonSans.ttf',
'I' => 'BentonSans-Bold.ttf',
],
],
'default_font' => 'BentonSans',
]
);