Home > Article > Backend Development > How to solve Chinese garbled characters in php pdf
php PDF Chinese garbled solution: 1. Add "require_once(dirname(FILE).'/lang/chi.php');" in "tcpdf/config/tcpdf_config.php"; 2. Modify the defined values of two constants.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php pdf How to solve Chinese garbled characters?
php generates pdf garbled characters?php tcpdf generates pdf: Chinese garbled characters
TCPDF is a good library for generating PDF. Unfortunately, the official support for East Asian fonts including Chinese is not very good. Kind of.
Scenario: A certain project needs to generate invoices in pdf format based on database information. Consider using the stable tcpdf. Although there are many other options, this should be the first choice. I won’t mention the reasons.
But I discovered something: when there is Chinese information on the invoice, it is garbled and displayed as a question mark.
After searching on Google, I learned a little bit.
TCPDF does not adopt common self-explanatory standards such as ttf, but uses a special font format. There are articles copied everywhere on Google saying that Droid Sans that supports Chinese should be used, and they also say tcpdf with certainty There is a utils directory under the fonts folder, and there is ttf2num.exe under it. Maybe, this thing is in the old version of tcpdf, but it is not in the 6.0.X version I downloaded
However, after going through many difficulties and dangers, I finally converted Droid Sans into a font format acceptable to TCPDF (download the converted font: http://pan.baidu.com/s/1bnq21Ld).
The main file of tcpdf uses ASCII encoding. Don’t change it to UTF-8 format on your own.
The data passed to tcpdf should not be in utf-8 format, otherwise, Chinese errors will occur.
Extract the downloaded font compressed package above and get three files:
droidsansfallback.php, droidsansfallback.z and droidsansfallback.ctg.z. Copy these three files to tcpdf /fonts directory.
#Personal test, this line is not required
In addition to the above points, you also need to add in tcpdf/config/tcpdf_config.php:
require_once(dirname(FILE).’/lang/chi.php’);
Modify this file The definition values of the two constants are:
define (‘PDF_FONT_NAME_MAIN’, ‘droidsansfallback’); define (‘PDF_FONT_NAME_DATA’, ‘droidsansfallback’);
OK, it can already support the output of pdf documents containing Chinese!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve Chinese garbled characters in php pdf. For more information, please follow other related articles on the PHP Chinese website!