d4ta
d4ta

Reputation: 75

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.

Upvotes: 4

Views: 7400

Answers (2)

Stewart Kirkpatrick
Stewart Kirkpatrick

Reputation: 1

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);

Upvotes: 0

Muhammad Abdul-Rahim
Muhammad Abdul-Rahim

Reputation: 2010

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();

Upvotes: 7

Related Questions