Solved

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

Posted on 2013-05-15
2
3,485 Views
Last Modified: 2013-05-18
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!
0
Comment
Question by:simdex
2 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39169828
From http://www.fpdf.org/ :

The class can produce documents in many languages other than the Western European ones: Central European, Cyrillic, Greek, Baltic and Thai, provided you own TrueType or Type1 fonts with the desired character set. Chinese, Japanese and Korean are supported too.
The most common problem is that the fonts you have available to embed in the PDF do not have the characters / symbols that you need.  Just generating the code in UTF-8 is not enough.  Many fonts won't have the characters you need.

If you click on the Chinese link on the FPDF home page, there is additional info and a dowload to enable Chinese.

Also from the FAQ page:
Don't use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39170118
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

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now