[英]Custom font in mPDF won´t load
我正在使用 mPDF 7.x 版並嘗試遵循此文檔: https ://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html
我只是無法讓它工作。 沒有錯誤,但字體仍然是默認的 mPDF 字體。 我還嘗試使用以下答案以另一種方式做到這一點:
如何使用 mPDF 生成 PDF 並向其添加自定義 Google 字體?
但我想它們不起作用,因為它們可能只適用於比 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 文件位於同一文件夾中的“字體”中。
這是 文檔中指示的實際工作解決方案:“在 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'
]
);
問題是在 Mpdf 版本 7 中,配置作為單個參數(數組)傳遞,而您將多個參數傳遞給構造函數。
這是一個有效的配置:
$mpdf = new \Mpdf\Mpdf(
[
'tempDir' => __DIR__ . '/upload',
'fontdata' => $fontData + [
'bentonsans' => [
'R' => 'BentonSans.ttf',
'I' => 'BentonSans-Bold.ttf',
],
],
'default_font' => 'BentonSans',
]
);
得到它的工作。 不知道有什么技巧,但這是工作代碼:
<?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');
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.