使用 DOMpdf 显示日语字符
Displaying Japanese Characters using DOMpdf
问题:
我想使用 DomPDF 生成包含日语文本的 pdf。为此,我知道我需要一种支持日语字符的字体并将该字体加载到 DomPDF 中。
我已经阅读了 DomPDF 的 UnicodeHowTo 和关于 SO 的几个类似问题,但我似乎无法让它工作。我在应该是日语字符的地方得到 ?
。
在我的 dompdf 配置中,我设置了 def("DOMPDF_UNICODE_ENABLED", true);
,它在管理界面中也显示为 true。我使用的字体(我试过几种)应该支持日文字符,并使用 load_font.php
脚本加载到 DomPDF 中。为了完整起见,我必须注意生成字体文件会给我 2 个相同的警告
Warning: strftime(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected the timezone 'UTC' for
now, but please set date.timezone to select your timezone. in ..
最小(不)工作示例:
相关 html 存储在 PHP 变量中 $template
:
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<META http-equiv="X-UA-Compatible" content="IE=8">
<TITLE>Test voucher</TITLE>
<STYLE type="text/css">
@font-face {
font-family: \'mgenplus\';
font-style: normal;
font-weight: 400;
src: url(dompdf/fonts/rounded-mgenplus-1c-regular.ttf) format(\'truetype\');
}
.ft0{font: 14px;line-height: 16px;}
*{ font-family: mgenplus !important;}
</STYLE>
</HEAD>
<BODY>
<P>ねん だい かい にほんごのうりょくしけん</P>
<!-- .... -->
相关PHP:
require_once("dompdf/dompdf_config.inc.php");
$template = mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8');
$dompdf = new DOMPDF();
$dompdf->load_html($template, 'UTF-8');
$dompdf->render();
$dompdf->stream("test.pdf",array('Attachment'=>0));
如果有人有任何建议或看看我是否遗漏了什么,我们将不胜感激!
没关系,我发现了它不起作用的原因,我会post在这里为遇到同样问题的任何人提供它。
问题出在这一段:
@font-face {
font-family: \'mgenplus\';
font-style: normal;
font-weight: 400;
src: url(dompdf/fonts/rounded-mgenplus-1c-regular.ttf) format(\'truetype\');
}
.ft0{font: 14px;line-height: 16px;}
*{ font-family: mgenplus !important;}
我必须删除 @font-face
块,不需要它,因为您是从 DomPDF 而不是从文件加载字体。造成所有麻烦的行是 .ft0{font: 14px;line-height: 16px;}
它显然将字体系列设置为浏览器默认值并且 DomPDF 没有考虑之后的覆盖(不支持 !important
?)。
将行更改为 .ft0{font-size: 14px;line-height: 16px;}
解决了我的问题。
问题:
我想使用 DomPDF 生成包含日语文本的 pdf。为此,我知道我需要一种支持日语字符的字体并将该字体加载到 DomPDF 中。
我已经阅读了 DomPDF 的 UnicodeHowTo 和关于 SO 的几个类似问题,但我似乎无法让它工作。我在应该是日语字符的地方得到 ?
。
在我的 dompdf 配置中,我设置了 def("DOMPDF_UNICODE_ENABLED", true);
,它在管理界面中也显示为 true。我使用的字体(我试过几种)应该支持日文字符,并使用 load_font.php
脚本加载到 DomPDF 中。为了完整起见,我必须注意生成字体文件会给我 2 个相同的警告
Warning: strftime(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in ..
最小(不)工作示例:
相关 html 存储在 PHP 变量中 $template
:
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<META http-equiv="X-UA-Compatible" content="IE=8">
<TITLE>Test voucher</TITLE>
<STYLE type="text/css">
@font-face {
font-family: \'mgenplus\';
font-style: normal;
font-weight: 400;
src: url(dompdf/fonts/rounded-mgenplus-1c-regular.ttf) format(\'truetype\');
}
.ft0{font: 14px;line-height: 16px;}
*{ font-family: mgenplus !important;}
</STYLE>
</HEAD>
<BODY>
<P>ねん だい かい にほんごのうりょくしけん</P>
<!-- .... -->
相关PHP:
require_once("dompdf/dompdf_config.inc.php");
$template = mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8');
$dompdf = new DOMPDF();
$dompdf->load_html($template, 'UTF-8');
$dompdf->render();
$dompdf->stream("test.pdf",array('Attachment'=>0));
如果有人有任何建议或看看我是否遗漏了什么,我们将不胜感激!
没关系,我发现了它不起作用的原因,我会post在这里为遇到同样问题的任何人提供它。
问题出在这一段:
@font-face {
font-family: \'mgenplus\';
font-style: normal;
font-weight: 400;
src: url(dompdf/fonts/rounded-mgenplus-1c-regular.ttf) format(\'truetype\');
}
.ft0{font: 14px;line-height: 16px;}
*{ font-family: mgenplus !important;}
我必须删除 @font-face
块,不需要它,因为您是从 DomPDF 而不是从文件加载字体。造成所有麻烦的行是 .ft0{font: 14px;line-height: 16px;}
它显然将字体系列设置为浏览器默认值并且 DomPDF 没有考虑之后的覆盖(不支持 !important
?)。
将行更改为 .ft0{font-size: 14px;line-height: 16px;}
解决了我的问题。