php 带有西里尔字符的 DOMPDF 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/990181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DOMPDF problem with Cyrillic characters
提问by Vladimir Kadalashvili
I am using the DOMPDF library to create an invoice in PDF. This document can be in French, Russian or English, but I am having trouble printing Russian characters.
我正在使用 DOMPDF 库以 PDF 格式创建发票。该文档可以是法语、俄语或英语,但我无法打印俄语字符。
First, I tried to use UTF-8 encoding and placed the metatag in the head of the HTML page to be converted:
首先,我尝试使用UTF-8编码,并将meta标签放在要转换的HTML页面的头部:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
But that didn't work.
但这没有用。
Then I inserted this metatag inside the BODYtag, and it helped solve the problem with French characters.
然后我meta在BODY标签中插入了这个标签,它帮助解决了法语字符的问题。
But Russian characters still don't work. I have also tried to convert Russian characters into HTML entities, but that too does not work.
但是俄语字符仍然不起作用。我还尝试将俄语字符转换为 HTML 实体,但这也不起作用。
I use R&OS CPDF class, not PDFLib as a backend.
我使用 R&OS CPDF 类,而不是 PDFLib 作为后端。
Can anyone help?
任何人都可以帮忙吗?
采纳答案by Artjom Kurapov
Problem is with fonts default dompdf uses (that is it doesn't have all unicode characters, whick are by now over 5000). Usually arialuni.ttf is what you need. You can download localized russian version at http://chernev.ru/dompdf.rar{broken link}
问题在于 dompdf 默认使用的字体(即它没有所有的 unicode 字符,现在超过 5000 个)。通常 arialuni.ttf 就是你所需要的。您可以在http://chernev.ru/dompdf.rar{broken link}下载本地化的俄语版本
Updated link: https://code.google.com/p/ipwn/downloads/detail?name=arialuni.ttf
更新链接:https: //code.google.com/p/ipwn/downloads/detail?name=arialuni.ttf
回答by Andrii Nemchenko
In accepted answer link is broken and it contained old version of DOMPDF.
在接受的答案中,链接已损坏,其中包含旧版本的 DOMPDF。
To work with unicode symbols in DOMPDF 0.6 you have two alternatives: use existed fonts or create your own font.
要在 DOMPDF 0.6 中使用 unicode 符号,您有两种选择:使用现有字体或创建自己的字体。
Use existed font (applied for DOMPDF 0.6):
- Downloadarchive and extract.
- Copy extracted files in your dompdf fonts folder
/dompdf/lib/fonts/. - Edit
dompdf_font_family_cache.dist.phpwith snippet 1. - In CSS use
font-family: times;.
使用现有字体(适用于 DOMPDF 0.6):
- 下载存档并解压。
- 将提取的文件复制到 dompdf 字体文件夹中
/dompdf/lib/fonts/。 dompdf_font_family_cache.dist.php使用片段 1 进行编辑。- 在 CSS 中使用
font-family: times;.
Snippet 1:
片段 1:
/* ... */
'times' => array (
'normal' => DOMPDF_FONT_DIR . 'times',
'bold' => DOMPDF_FONT_DIR . 'timesbd',
'italic' => DOMPDF_FONT_DIR . 'timesi',
'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'
),
'times-roman' => array (
'normal' => DOMPDF_FONT_DIR . 'times',
'bold' => DOMPDF_FONT_DIR . 'timesbd',
'italic' => DOMPDF_FONT_DIR . 'timesi',
'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'
),
/* ... */
If you want to use your own TTF font (say,
Arial.ttf):- Run:
ttf2afm -o Arial.afm Arial.ttf. (I did it in Ubuntu.) - Run:
ttf2ufm -a -F Arial.ttf. (I did it in Windows using exe from UFPDF, but I guess you can use/dompdf/lib/ttf2ufm/bin/ttf2ufm.exe.) - Copy
Arial.*files in/dompdf/lib/fonts/. - Add to
dompdf_font_family_cache.dist.phpsnippet 2. - In CSS use
font-family: arial;.
- Run:
如果您想使用自己的 TTF 字体(例如,
Arial.ttf):
Snippet 2:
片段 2:
/* ... */
'arial' => array (
'normal' => DOMPDF_FONT_DIR . 'Arial',
'bold' => DOMPDF_FONT_DIR . 'Arial',
'italic' => DOMPDF_FONT_DIR . 'Arial',
'bold_italic' => DOMPDF_FONT_DIR . 'Arial'
)
/* ... */
回答by nfort
if you will use DejaVu font you can see cyrillic characters
如果您将使用 DejaVu 字体,您可以看到西里尔字符
The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans).
默认情况下,DejaVu TrueType 字体为 dompdf 提供了良好的 Unicode 字符覆盖范围。要使用 DejaVu 字体,请参考样式表中的字体,例如 body { font-family: DejaVu Sans; }(对于 DejaVu Sans)。
DOMPDF include DejaVu font be default
DOMPDF 默认包含 DejaVu 字体
$html = "<html><head><style>body { font-family: DejaVu Sans }</style>".
"<body>А вот и кириллица</body>".
"</head></html>";
$dompdf = new \DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
echo file_put_contents('cyrillic.pdf', $dompdf->output());
You can also set change deffor font by default in dompdf_config.inc.php
您还可以设置改变画质默认为字体dompdf_config.inc.php
def("DOMPDF_DEFAULT_FONT", "DejaVu Sans");
回答by qmic
Download arialuni.ttf Run php load_font.php 'Arial' arialuni.ttf in dompdf directory, set font to arial It works ;)
下载 arialuni.ttf 在 dompdf 目录中运行 php load_font.php 'Arial' arialuni.ttf,将字体设置为 arial 即可;)
回答by Hardoman
For me the 4 steps above didn't resolve the issue. Besides that, dompdf converts created pdf to ANSI (ISO) You need to disable this on the options page http://domain.com/admin/settings/print/pdf
对我来说,上面的 4 个步骤没有解决问题。除此之外,dompdf 将创建的 pdf 转换为 ANSI (ISO) 您需要在选项页面http://domain.com/admin/settings/print/pdf上禁用它
Tick the checkbox Use dompdf's Unicode Mode. This will force to create files in UTF-8/Unicode.
勾选复选框使用 dompdf 的 Unicode 模式。这将强制以 UTF-8/Unicode 创建文件。
Please note that web settings override settings in dompdf_config.inc.php by default.
请注意,默认情况下,Web 设置会覆盖 dompdf_config.inc.php 中的设置。
回答by dmpol18
Noted that problem could be in css-reset usage, particularly
font:inherit;
注意到问题可能出在 css-reset 使用中,特别是
font:inherit;
回答by B.Asselin
In your html, use this style :
在您的 html 中,使用此样式:
<style type="text/css">
* {
/*font-family: Helvetica, sans-serif;*/
font-family: "DejaVu Sans", sans-serif;
}
</style>

