php dompdf中文乱码怎么办

2023-02-09,

php dompdf中文乱码的解决办法:1、打开前端文件;2、通过设置“@font-face
{font-family:ttt;src: url(/static/font/simkai.ttf)}”引用中文字体;3、删除“dompdf_font_family_cache.php”即可。

本教程操作环境:Windows10系统、PHP8.1版、DELL G3电脑

php dompdf中文乱码怎么办?

php导出pdf,dompdf中文字体乱码解决办法(特别是代码迁移引起的乱码)

dompdf\lib\fonts\dompdf_font_family_cache.php记住这个文件里面存放的是字体生成的缓存,迁移时如果覆盖了这个文件会导致乱码而且很难找到出错的地方,相信我。。。。

dompdf是php把html导出pdf的工具,十分好用,除了个别css3的属性不支持,其他都非常不错,中文需要引用中文字体,具体办法官方有这里简单说下:

代码:

$html = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
@font-face
{
font-family:ttt;
src: url(/static/font/simkai.ttf)
}
body {
font-family: ttt, sans-serif;
}</style></head>
<body >
<div style="font-family:ttt;">
测 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试</div></body>
</div></body>
</html>';
//echo $html;exit;
$options = new Options();
$options->set('enable_remote', TRUE);
$dompdf = new Dompdf($options);
$dompdf->load_html( $html, 'UTF-8');;
// $dompdf->load_html( $html, ‘GBK‘);
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment"=>0));exit;

结果:

需要说明:

10.@font-face

{

font-family:ttt;

src: url(/static/font/simkai.ttf)

}这里的ttf是字体,位置放在static/font下,在html里可以直接用font-family:ttt;来引用;

2.就是这个文件dompdf\lib\fonts\dompdf_font_family_cache.php

如果迁移代码的时候需要把这个删掉,或者修改family的名称。

推荐学习:《PHP视频教程》

以上就是php dompdf中文乱码怎么办的详细内容,更多请关注北冥有鱼其它相关文章!

本文转载自【PHP中文网】,希望能给您带来帮助,苟日新、日日新、又日新,生命不息,学习不止。

《php dompdf中文乱码怎么办.doc》

下载本文的Word格式文档,以方便收藏与打印。