Tcpdf is a PHP5 function package that is used to quickly generate PDF files. Tcpdf is extended and improved based on fpdf. Support for Utf-8,unicode,html and XHTML. Using it to output PDF files is a great choice in PHP-based Web applications. But after all, this open source software is developed by foreigners, the support for Chinese is not always so satisfactory, so we need to further enhance it.
The first thing to download is tcpdf. To access the http://www.tcpdf.org, click the "Download" link on the navigation bar to download the latest version of the Tcpdf compact package. Because it contains examples and fonts, the file is about 10M. After the download, unzip it, you will get a directory named Tcpdf,
Tcpdf directory structure diagram
Move this directory to the root of the Apache Web page (assuming you are using Apache, and use 80 ports, if you have different changes according to the actual situation), open the browser, access http://localhost/tcpdf/should be able to see the list of files and folders in this directory. Since Tcpdf already has some examples to save time, we can modify and reuse the examples directly. Open the Tcpdf directory under the examples directory, there are more than 50 sample files, where example_038.php is used to test East Asian fonts (such as simplified/Traditional Chinese, Japanese, etc.), open this file, find $pdf->setfont this line, Modified to:
$pdf->setfont (' stsongstdlight ', ', 20);
The purpose of this line of code is to set the font and font size used in the body of the PDF. where "stsongstdlight" means "stsongstdlight" font, which is the default Simplified Chinese font for Adobe Reader, the configuration file for this font has been built into tcpdf, and we just need to call it directly. Next, change the $pdf->cell section to:
$pdf->cell (0, 10, ' agile bristle The fox jumps over the lazy dog. ', ', 1, 1, ' C '); $pdf->cell (0, ' The quick brown fox jumps over the lazy dog. ', ' ', 1, 1, ' C '); $pdf->cell (0, ' 1234567890 ', ' ', 1, 1, ' C ');
Save and then visit http://localhost/tcpdf/examples/example_038.php to generate a PDF document:
PDF file generated using the default Chinese font
The advantage of the PDF file generated in this way is that the file size is small and fast to generate. However, the disadvantage is that no Chinese fonts are embedded, only Adobe Reader is installed to display properly. What if the user is using Foxit Reader or a Linux operating system? The display effect is different. Therefore, embedding a font is required to ensure that the resulting PDF file has the same display effect in any environment. There are many Chinese fonts under windows, but the Chinese fonts we want to use in Tcpdf have the following requirements:
Unicode is supported because Tcpdf supports Unicode, the smaller the better, and the better support for Traditional Chinese;
In this way, Microsoft Jas and founder of some of the fonts are in line with the requirements. But they are all commercial fonts, and the size is not small, with Microsoft ya Black For example, Msyh.ttf file is more than 10M, if you use it, the resulting PDF file volume will be very large. Overall, I think the "Droid Sans Fallback" font meets the requirements:
First it is free font, second it is Unicode encoding, support simplified Traditional Chinese and Japanese Korean, etc., then it is small, not more than 5M.
However, Tcpdf does not support TTF font files, so let's convert it to tcpdf supported format before using it. There is a fonts subdirectory under the Tcpdf directory, and there is a utils in this subdirectory with a font conversion tool Ttf2ufm.exe. Here are the steps to convert: It's easy to find the download link to the Droid Sans fallback font on the web, so there's no need to do more description here. We copy the downloaded Droidsansfallback.ttf to the tcpdf\fonts\utils below, then open the Windows command line, switch to this path, enter the following command:
D:\www\tcpdf\fonts\utils>ttf2ufm-a-F Droidsansfallback.ttf
Wait a while until the Command line window displays
"Finished-font files created"
After that, you can see that the three files of DROIDSANSFALLBACK.AFM,DROIDSANSFALLBACK.T1A and DROIDSANSFALLBACK.UFM are generated under this directory. On the command line, enter
"C:\Program Files\wamp\php5\php.exe"-Q makefont.php Droidsansfallback.ttf DROIDSANSFALLBACK.UFM
Note: Here the "C:\Program Files\wamp\php5\php.exe" is the path of the Php.exe file, please enter according to the actual situation. After the carriage return runs, wait a moment, the command line window will prompt
Font definition file generated
That's it. Copy the generated droidsansfallback.php, Droidsansfallback.z, and droidsansfallback.ctg.z three files to the tcpdf\fonts below. Open a example_038.php file and
$pdf->setfont (' stsongstdlight ', ', 20);
Revision changed to
$pdf->setfont (' Droidsansfallback ', ', 20);
This makes it possible to invoke the font we just generated and then access http://localhost/tcpdf/examples/example_038.php to see the regenerated PDF document.
Tcpdf Support Chinese