使用Mpdf 8.0.8,我创建了一个具有自定义字体的文档,该文档显示良好,但不间断空格除外,它显示为交叉方块:
使用 FontForge 打开我的字体时,我看到
uni00A0
字符已定义(它是空白的,但没有十字)
这是调用Mpdf的代码:
public function getPdfData($values, $medias, $locale = 'fr_FR')
{
$sessionLocale = $this->translator->getLocale();
$this->translator->setLocale($locale);
$html = $this->twig->render('@Pim/testPdf.html.twig');
$this->translator->setLocale($sessionLocale);
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$fontData = $fontData + [
'myhelvet' => [
'R' => 'helvetlight.ttf',
'I' => 'helvetthick.ttf',
],
'timeslightitalic' => [
'R' => 'timeslightitalic.ttf'
]
];
$defaultFont = 'myhelvet';
$mpdf = new \Mpdf\Mpdf([
'orientation' => 'L',
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 1,
'margin_bottom' => 0,
'fontDir' => array_merge($fontDirs, [
__DIR__ . '/../Resources/public/fonts',
]),
'fontdata' => $fontData,
'default_font' => $defaultFont,
'mode' => '+aCJK',
"autoScriptToLang" => true,
"autoLangToFont" => true,
]);
$mpdf->addFontDirectory(__DIR__ . '/../Resources/public/fonts');
$mpdf->WriteHTML($html);
$pdfData = $mpdf->Output('', 'S');
return $pdfData;
}
这里的 html 视图无法渲染由 Php NumberFormatter 创建的不间断空格:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="font-family: myhelvet">Formatting a number: {{ '20000'|numberFormatLocale('fr_FR') }}</div>
</body>
</html>
最后,生成分隔符的 php 代码(Twig 过滤器):
public static function numberFormatLocale($number, $locale)
{
$formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
return $formatter->format($number);
}
NumberFormatter 的输出不是
U+00A0
,而是 0xE2 0x80 0xAf
,或 U+202F
- NARROW NO-BREAK SPACE,这很可能不受字体支持。