[英]DomPDF generation for chinese characters
我正在尝试使用dompdf生成一个包含汉字的PDF。 这是我的代码:
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中看起来还可以。
有任何建议吗?
首先,移动def("DOMPDF_UNICODE_ENABLED", true);
高于要求,因为dompdf包含的def
函数仅在常量不存在的情况下才定义该常量。 当包含dompdf_config.inc.php时,该常数将在那时设置。 另外,请改用define
。
其次,您需要其他字体。 dompdf随附的DejaVu字体不支持CJK。 有关概述,请阅读我对Dompdf的回答并设置不同的font-family 。
如果当前没有字体,则可以尝试查找Firefly Sung 。 您决定使用的任何字体都应该是TTF,以便dompdf使用它。
这是一个通过@ font-face CSS规则使用Firefly Sung的示例,无需安装:
<!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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.