汉字的DomPDF生成 [英] DomPDF generation for chinese characters
问题描述
我正在尝试使用dompdf生成一个包含汉字的PDF. 这是我的代码:
I am trying to generate a PDF that will contain Chinese characters using dompdf. Here is my code:
require('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);
$html = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
*{ font-family: DejaVu Sans, font-size: 12px;}
</style> </head> <body>
忠烈祠
</body>
</html>';
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
$filename = 'a.pdf';
$path = $filename;
file_put_contents($path, $output);
问题是当我使用chrome或adobe reader打开它时,生成的PDF仅显示正方形,但是在Mozilla firefox中看起来还可以.
The problem is the the generated PDF show only squares when i open it with chrome or adobe reader, but it looks ok in Mozilla firefox.
有没有建议?
推荐答案
首先,将def("DOMPDF_UNICODE_ENABLED", true);
移到要求上方,因为dompdf附带的def
函数仅定义该常量(如果不存在).当包含dompdf_config.inc.php时,该常数将在那时设置.另外,请改用define
.
First, move def("DOMPDF_UNICODE_ENABLED", true);
above the require since the def
function included with dompdf only defines the constant if it doesn't exist. When you include dompdf_config.inc.php that constant will be set at that time. Also, use define
instead.
第二,您需要其他字体. dompdf随附的DejaVu字体不支持CJK.有关概述,请阅读我对 Dompdf的回答,并设置其他字体系列.
Second, you need a different font. The DejaVu fonts included with dompdf do not support CJK. For an overview read my answer to Dompdf and set different font-family.
如果您当前没有字体,则可以尝试查找 Firefly Sung .您决定使用的任何字体都应该是TTF,以便dompdf使用它.
If you do not currently have a font you might try looking up Firefly Sung. Whatever font you decide to use it should be TTF in order for dompdf to use it.
以下是通过@ font-face CSS规则使用Firefly Sung的示例,无需安装:
Here's an example using Firefly Sung via the @font-face CSS rule, no installation required:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
@font-face {
font-family: 'Firefly Sung';
font-style: normal;
font-weight: 400;
src: url(http://eclecticgeek.com/dompdf/fonts/cjk/fireflysung.ttf) format('truetype');
}
* {
font-family: Firefly Sung, DejaVu Sans, sans-serif;
}
</style>
</head>
<body>
<div>忠烈祠</div>
</body>
</html>
这篇关于汉字的DomPDF生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!