Home > Article > Backend Development > What to do if php dompdf Chinese garbled characters
php dompdf Chinese garbled solution: 1. Open the front-end file; 2. Set "@font-face" {font-family:ttt;src: url(/static/font/simkai.ttf)}" refers to the Chinese font; 3. Delete "dompdf_font_family_cache.php".
##The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
What should I do if dompdf has Chinese garbled characters?
php exports pdf, Solution to garbled Chinese fonts in dompdf (especially garbled characters caused by code migration) dompdf\lib\fonts\dompdf_font_family_cache.php Remember that this file stores the cache generated by the font. If this file is overwritten during migration It will cause garbled code and it is difficult to find the error, believe me... dompdf is a tool for exporting html to pdf from php. It is very easy to use. Except for a few css3 attributes that are not supported, everything else is very good. Chinese needs to quote Chinese fonts. The specific method is briefly explained here: Code:$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;Result:
##Need to explain:
10.@font-face
{
font-family:ttt;
src: url(/static/font/simkai.ttf)
}The ttf here is the font, and the location is placed under static/font. In HTML, you can directly use font-family:ttt; to reference it;
2. This is the file dompdf\lib \fonts\dompdf_font_family_cache.php
If you need to delete this when migrating the code, or change the name of the family.
Recommended study: "
PHP video tutorialThe above is the detailed content of What to do if php dompdf Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!