简体   繁体   中英

Issues with Chinese characters in FPDF

I'm generating some order confirmations using FPDF library, which works great. But now I need some Chinese characters in the document. I have searching for many hours by now and have found no workable solution. Is it possible supporting Chinese characters using FPDF or is there an alternative to FPDF which will support it?

All the characters are mixed with regular text and are stored in a MySQL database. The PDF document only show unicode when printing the Chinese characters.

There exist Chinese encoding formats like Big5 or GBK . However, more likely than not, the text you are trying to input is in Unicode. There exists tFPDF , which provides Unicode support. I will test printing the following traditional hanzi: 中國. This means China, for those reading who do not know.

//  Remember to copy msjh.ttf to [path to tFPDF]/font/unifont/ directory

//  Initialize tFPDF
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();

//  Add a Unicode font like MSJH
$pdf->AddFont('MSJH','','msjh.ttf',true);
$pdf->SetFont('MSJH','',42);

//  Output Chinese string to PDF
$pdf->Text(12,42,"中國");

//  Output PDF document
$pdf->Output();

Another alternative is the Code200365k TTF

$pdf->AddFont('Code200365k','','Code200365k.ttf',true);

I use it for Chinese characters only as follows:

if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $my_value)){
    $pdf->SetFont('Code200365k','',12);
}
$pdf->Cell(130,9,$my_value,1,1,'L');
$pdf->SetFont('Arial','',11);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM