【问题标题】:generating pdf with MPDF, non breaking space displayed as unsupoprted characters使用 MPDF 生成 pdf,非中断空间显示为不支持的字符
【发布时间】:2021-02-11 10:12:39
【问题描述】:

使用 Mpdf 8.0.8,我创建了一个自定义字体的文档,该字体显示正常,除了不间断空格,显示为交叉方块:

当使用 FontForge 打开我的字体时,我看到 uni00A0 char 已定义(它是空白但没有交叉)

这里是调用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;
}

这里是无法呈现由 Php NumberFormatter 创建的不间断空间的 html 视图:

<!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);
}

【问题讨论】:

  • @Finwe 谢谢。我编辑了我的问题以显示我的代码。
  • 没有 HTML 数据,几乎是不完整的。
  • @Finwe 这里是 HTML,它是非常基本的(中断空间是由 PHP 生成的)
  • @Finwe 如果你愿意,我可以在 FontForge 中添加打开字体的屏幕截图

标签: fonts char mpdf


【解决方案1】:

NumberFormatter 的输出不是U+00A0,而是0xE2 0x80 0xAf,或者U+202F - NARROW NO-BREAK SPACE 这很可能是字体不支持的。

【讨论】:

  • 非常感谢您的时间和洞察力。我在这个问题上花了几个小时,但我对字体/字符代码的理解非常有限。我在我的字体中添加了带有 FontForge 的字符,这解决了问题。
猜你喜欢
  • 2016-04-25
  • 1970-01-01
  • 2019-10-08
  • 2015-08-07
  • 1970-01-01
  • 2020-02-08
  • 2022-06-10
  • 2018-02-04
  • 2023-03-29
相关资源
最近更新 更多