4

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.

Brian
  • 14,610
  • 7
  • 35
  • 43
d4ta
  • 75
  • 1
  • 6
  • Did you checked [this](http://fpdf.org/phorum/read.php?f=1&i=5142&t=5142) out? – Jan Slabon May 21 '15 at 06:37
  • @MariM But the input encoding is correct? – Jan Slabon May 21 '15 at 15:33
  • The answer by Muhammad Abdul-Rahim is fundamentally correct. I provided a more detailed solution here, though, where I describe usable Chinese fonts and full-Unicode alternatives, with Google Noto font, etc., Take a look at my answer here: https://stackoverflow.com/a/56429391/2430549 – HoldOffHunger Jun 04 '19 at 16:41

2 Answers2

7

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();
Muhammad Abdul-Rahim
  • 1,980
  • 19
  • 31
  • For those of you somehow confused as me: Microsoft offers MSJH font as a free download here: https://www.microsoft.com/en-us/download/details.aspx?id=12072 It seems it's intended for Windows XP users; if you get an error while trying to run the downloaded file (it's an .exe!), you can uncompress it simply with 7Zip or so. – Pere Jun 07 '16 at 08:25
0

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