13

我正在使用 DOMPDF 库以 PDF 格式创建发票。此文档可以是法语、俄语或英语,但我在打印俄语字符时遇到了问题。

首先,我尝试使用 UTF-8 编码,并将meta标签放在要转换的 HTML 页面的头部:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

但这没有用。

然后我在meta标签内插入了这个标签BODY,它帮助解决了法语字符的问题。

但是俄语字符仍然不起作用。我也尝试将俄语字符转换为 HTML 实体,但这也不起作用。

我使用 R&OS CPDF 类,而不是 PDFLib 作为后端。

任何人都可以帮忙吗?

4

7 回答 7

13

如果您将使用 DejaVu 字体,您可以看到西里尔字符

DejaVu TrueType 字体已预先安装,默认情况下为 dompdf 提供不错的 Unicode 字符覆盖。要使用 DejaVu 字体,请参考样式表中的字体,例如 body { font-family: DejaVu Sans; }(对于 DejaVu Sans)。

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());

您还可以在dompdf_config.inc.php中默认为字体设置更改def

def("DOMPDF_DEFAULT_FONT", "DejaVu Sans");
于 2015-11-08T12:25:28.903 回答
12

在接受的答案链接已损坏,它包含旧版本的 DOMPDF。

要在 DOMPDF 0.6 中使用 unicode 符号,您有两种选择:使用现有字体或创建自己的字体。

  • 使用现有字体(适用于 DOMPDF 0.6):
  1. 下载存档并提取。
  2. 将提取的文件复制到您的 dompdf 字体文件夹/dompdf/lib/fonts/中。
  3. dompdf_font_family_cache.dist.php使用片段 1 进行编辑。
  4. 在 CSS 中使用font-family: times;.

片段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'
),
/* ... */

  • 如果您想使用自己的 TTF 字体(例如Arial.ttf):
  1. 运行:ttf2afm -o Arial.afm Arial.ttf。(我是在 Ubuntu 中完成的。)
  2. 运行:ttf2ufm -a -F Arial.ttf。(我在 Windows 中使用UFPDF中的 exe 完成了此操作,但我想您可以使用/dompdf/lib/ttf2ufm/bin/ttf2ufm.exe.)
  3. Arial.*文件复制到/dompdf/lib/fonts/.
  4. 添加到dompdf_font_family_cache.dist.php片段 2。
  5. 在 CSS 中使用font-family: arial;.

片段 2:

/* ... */
'arial' => array (
    'normal' => DOMPDF_FONT_DIR . 'Arial',
    'bold' => DOMPDF_FONT_DIR . 'Arial',
    'italic' => DOMPDF_FONT_DIR . 'Arial',
    'bold_italic' => DOMPDF_FONT_DIR . 'Arial'
)
/* ... */
于 2011-10-06T09:45:44.993 回答
8

问题在于默认 dompdf 使用的字体(即它没有所有的 unicode 字符,现在已经超过 5000 个)。通常 arialuni.ttf 是你需要的。您可以在http://chernev.ru/dompdf.rar {broken link}下载本地化的俄语版本

更新链接:https ://code.google.com/p/ipwn/downloads/detail?name=arialuni.ttf

于 2009-11-06T11:28:39.047 回答
0

下载 arialuni.ttf 在 dompdf 目录中运行 php load_font.php 'Arial' arialuni.ttf,将字体设置为 arial 就可以了;)

于 2010-12-29T23:28:42.360 回答
0

对我来说,上面的 4 个步骤并没有解决问题。除此之外,dompdf 将创建的 pdf 转换为 ANSI (ISO) 您需要在选项页面 http://domain.com/admin/settings/print/pdf上禁用此功能

勾选使用 dompdf 的 Unicode 模式复选框。这将强制以 UTF-8/Unicode 创建文件。

请注意,默认情况下 Web 设置会覆盖 dompdf_config.inc.php 中的设置。

于 2014-12-24T22:48:37.840 回答
0

注意到问题可能出在 css-reset 使用中,特别是 font:inherit;

于 2016-10-19T12:36:17.597 回答
0

在您的 html 中,使用以下样式:

<style type="text/css">
* {
  /*font-family: Helvetica, sans-serif;*/
  font-family: "DejaVu Sans", sans-serif;
}
</style>
于 2019-11-28T08:38:38.470 回答