使用 MPDF 生成 pdf,不间断 space 显示为不受支持的字符
generating pdf with MPDF, non breaking space displayed as unsupoprted characters
使用 Mpdf 8.0.8,我创建了一个自定义字体的文档,它显示良好,但不间断的 spaces 除外,它显示为交叉正方形:
当用 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 创建的非中断 space:
<!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 代码生成中断 space(一个 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 这很可能不受字体支持。
使用 Mpdf 8.0.8,我创建了一个自定义字体的文档,它显示良好,但不间断的 spaces 除外,它显示为交叉正方形:
当用 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 创建的非中断 space:
<!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 代码生成中断 space(一个 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 这很可能不受字体支持。