dompdf not displaying some of latin characters

狂风中的少年 提交于 2019-12-23 05:32:27

问题


I am using latest stable 0.6 dompdf version but I am unable to display some latin characters, I see that documentation is for now incomeplete.

So far I am trying to use Open Sans font, I have even converted it to afm format, but still some display are displayed with ? instead of our latin characters 'č, ć, đ, ž'.

Font is downloaded as ttf, and font definition is added to dompdf_font_family_cache.dist.php file as follows:

'open sans' => 
  array (
    'normal' => $rootDir . '/lib/fonts/OpenSans',
  ),

Outputing html contents to browser renders these characters fine. Is there any official solution, because load_font.php is missing or should update to 0.7-beta and use @font-face instead?


回答1:


Character Encoding

The default encoding of a PDF document is Windows ANSI (1). This encoding provides limited character set support that does not include the characters specified in the question (č, ć, đ, ž). So while the text is technically latin-encoded the specific encoding that supports those characters (ISO-8859-5) is not the same as the default one provided provided in PDF documents (Windows-1252, aka Windows ANSI, which is pretty much ISO-8859-1).

Font Metrics

With the character encoding issue in mind you took the right approach to add support for your characters by loading a font. The means by which you've done it, however, will not get you the expected results. dompdf (when using the CPDF backend) utilizes font metrics to determine how to lay out the text of a document. The font metrics come in two forms, AFM (Adobe Font Metrics) and UFM (Unicode Font Metrics). These two forms correspond to the two possible encodings supported by dompdf, Windows ANSI and Unicode. The fact that your font metrics are in AFM format indicates to dompdf that the font is encoded as Windows ANSI.

Loading Fonts

While you can modify the dompdf_font_family_cache.dist.php file it is not recommended. Since that file is included as part of the distribution any updates you perform could over-write the file. When using any of the supported font-loading methods dompdf will create a file called dompdf_font_family_cache.php to store name/location information for your custom fonts. If you want to tweak your custom font information you would do so in this file. This file is typically stored alongside the TTF and AFM files associated with the custom fonts (not necessarily in the dompdf/lib/fonts directory ... depending on your configuration).

I would recommend against manually editing dompdf_font_family_cache.php. Rather I would use the CSS @font-face rules to define and load fonts if for no other reason ease of use. If you're inclined to use a command line tool to pre-load your fonts the load_font.php script is still included in the 0.6.x releases of dompdf (not sure why you think otherwise). If you want to move to 0.7.0 you can find an updated version of the load_font.php script in the dompdf-utils project.

Lastly, if you continue to have trouble you can always try using one of the bundled DejaVu fonts (available starting with dompdf 0.6.0).


(1) OK, yes, there are a few possible encodings. But for the most part you can consider Windows ANSI the default.



来源:https://stackoverflow.com/questions/35048509/dompdf-not-displaying-some-of-latin-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!