FPDF is a PHP class library for generating PDF documents, which allows generating PDF files without using a PDF document editor.
However, when using FPDF to generate Chinese PDF documents, the problem of Chinese garbled characters may occur. This question also appears frequently in some website forums. How to solve this problem?
First, let’s understand the character set issue of PDF files. The character set contained in the PDF file is the Unicode character set, and the Chinese character codes are converted according to GB2312 or UTF-8 encoding. In the FPDF class library, only the ISO-8859-1 character set is supported by default, which causes the problem of Chinese PDF garbled characters. In this case, we can solve it by converting character sets or changing fonts.
Solution 1: Convert character set
We can use the iconv() function to convert the GB2312 or UTF-8 encoded Chinese string into the ISO-8859-1 character set and then output it. This will solve the problem of Chinese garbled characters. The specific method is as follows:
1. Before creating the font, use the iconv() function to convert the string into the ISO-8859-1 character set
$text = "中文内容"; $text_iso = iconv("UTF-8","ISO-8859-1",$text);
2. Use the generated ISO-8859- 1 string to create FPDF object
$pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','',14); $pdf->Cell(0,10,$text_iso); $pdf->Output();
At this time, in the generated PDF document, the Chinese content will not be garbled. However, this method requires repeated character set conversion when processing large pieces of Chinese content, which is not convenient enough.
Solution 2: Change the font
Changing the font and using a font that supports the Chinese character set is also an effective way to solve the problem of Chinese garbled characters. We can use the AddFont() function in FPDF to load font files that can support Chinese character sets and create fonts that support Chinese characters, thereby solving the problem of Chinese garbled characters.
1. Download fonts that support Chinese character sets
Search the Internet for "FPDF fonts that support Chinese characters" and you can find many download links, such as Founder fonts, Yahei fonts, etc. Place the downloaded font files in the "fonts" directory of FPDF.
2. Load font files
Use FPDF’s AddFont() function to load font files that support Chinese characters.
require('fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->AddFont('MSYH', '', 'msyh.ttf', true); $pdf->SetFont('MSYH', '', 14); $pdf->Cell(0,10, "中文内容"); $pdf->Output();
Among them, the AddFont() function has 4 parameters, namely: font name, font style, font file name, and whether it is a "trueType" font. Among them, the font style has four choices: '', 'B', 'I', and 'U', which represent normal, bold, italic, and underline respectively. Among fonts that support Chinese characters, there is usually only the normal style.
Note that when using the AddFont() function, the font file must be stored in the fonts directory and must have a .ttf suffix.
After using fonts that support the Chinese character set, the Chinese content in the generated PDF document has been displayed normally.
Summary
When using FPDF to generate Chinese PDF documents, you can use the above two methods to solve the problem of Chinese garbled characters.
The first method requires repeated character set conversion, which is not convenient enough. The second method requires downloading font files that support Chinese character sets and placing them in the "fonts" directory of FPDF. It is more convenient to use than the first method. At the same time, it should also be noted that Chinese typesetting in PDF documents may cause problems due to font size and other issues, so it is best to test before using Chinese content.
The above is the detailed content of How to solve the Chinese garbled problem in php fpdf. For more information, please follow other related articles on the PHP Chinese website!