Link to home
Start Free TrialLog in
Avatar of simdex
simdexFlag for United States of America

asked on

FPDF (PHP class for generating PDFs): Problem with Chinese character set – cannot properly print Chinese characters

Here is the problem:
 
We are using FPDF (a PHP class) to create PDF documents in a PHP script. We are able to write most European languages like English, Spanish, etc., but when we try to write Chinese characters, it does not work. It only prints garbage characters in the PDF file (with UTF-8). We know this has to do with character encoding. In addition to UTF-8, we have tried  Chinese character encoding GB18030, which totally blanks out the PDF, with no output at all.

Our developers already looked at these links/resources, but they didn't help:

http://blogs.adobe.com/jhsia/2005/09/how_to_create_p.html
http://stackoverflow.com/questions/8737772/tcpdf-encode-chinese-character
http://stackoverflow.com/questions/14382868/html2pdf-with-tcpdf-not-rendering-chinese-characters-in-final-pdf-document

Any ideas? Any thoughts are greatly appreciated. Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FPDF is a little old and long in the tooth.  You might consider TCPDF.  And in the spirit of full disclosure, I am only guessing because I have never tried to write Chinese characters in a PDF document.  Here is a slim-line Hello World in TCPDF.
http://www.laprbass.com/RAY_tcpdf_image_position.php

<?php // RAY_tcpdf_image_position.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// SYNTHESIZE THE PDF FILE NAME
$pdf_file_link    = '/RAY_tcpdf_example.pdf';
$pdf_file_name    = getcwd() . $pdf_file_link;

// THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC HEADER AND FOOTER
    public function Header() {}
    public function Footer() {}
}

echo "<br />Starting PDF creation" . PHP_EOL;

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0,0,0,TRUE);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// DO NOT DO THIS
// $pdf->setFillColor(255,255,224);


// CELL BORDERS, IF NEEDED
$bdr = '1TRBL';
$bdr = 0;

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

// ADD AN IMAGE
// http://www.tcpdf.org/doc/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352

$img = 'RAY_EE_images/ray_padded.png';

// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SIZE
$pdf->setImageScale(1.53);

// GET THE IMAGE, I HOPE
$pdf->Image
( $img
, 0             // $x
, 0             // $y
, 0             // WIDTH
, 0             // HEIGHT
, 'PNG'         // TYPE
, '#'           // LINK URL
, 'T'           // SET POINTER TOP LEFT
, FALSE         // NO RESIZING
, 300           // DPI
, 'L'           // PALIGN
, FALSE         // ISMASK
, FALSE         // IMGMASK
, 0             // BORDER
, FALSE         // FIT TO BOX
, FALSE         // HIDDEN
, FALSE         // FIT ON PAGE
)
;

// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24            // $x,
, 24            // $y,
, 'HELLO WORLD'
, FALSE         // $fstroke = false,
, FALSE         // $fclip = false,
, TRUE          // $ffill = true,
, $bdr          // $border = 0,
, 2             // $ln = 0, 2=PUT CURSOR BELOW
, ''            // $align = '', DEFAULT LEFT
, FALSE         // $fill = false, CELL BACKGROUND
, ''            // $link = '', NOT A LINK
, 0             // $stretch = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;

// WRITE THE PDF FILE TO THE SERVER
echo "<br />Writing PDF\n";
$pdf->Output($pdf_file_name,'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE LABELS
echo "<br /><a target=\"my_PDF\" href=\"$pdf_file_link\"><strong>Print the PDF</strong></a>";

Open in new window