Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 223
d4ta Avatar asked Mar 16 '23 00:03

d4ta


1 Answers

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();
like image 78
Muhammad Abdul-Rahim Avatar answered Mar 25 '23 07:03

Muhammad Abdul-Rahim