Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

php - How to generate pdf files _with_ utf-8 multibyte characters using Zend Framework

I've got a "little" problem with Zend Framework Zend_Pdf class. Multibyte characters are stripped from generated pdf files. E.g. when I write a?bc?de? it becomes abcd with lithuanian letters stripped.

I'm not sure if it's particularly Zend_Pdf problem or php in general.

Source text is encoded in utf-8, as well as the php source file which does the job.

Thank you in advance for your help ;)

P.S. I run Zend Framework v. 1.6 and I use FONT_TIMES_BOLD font. FONT_TIMES_ROMAN does work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Zend_Pdf supports UTF-8 in version 1.5 of Zend Framework. However, the standard PDF fonts support only the Latin1 character set. This means you can't use Zend_Pdf_Font::FONT_TIMES_BOLD or any other "built-in" font. To use special characters you must load another TTF font that includes characters from other character sets.

I use Mac OS X, so I tried the following code and it produces a PDF document with the correct characters.

$pdfDoc = new Zend_Pdf();
$pdfPage = $pdfDoc->newPage(Zend_Pdf_Page::SIZE_LETTER);

// load TTF font from Mac system library
$font = Zend_Pdf_Font::fontWithPath('/Library/Fonts/Times New Roman Bold.ttf');
$pdfPage->setFont($font, 36);

$unicodeString = 'a?bc?de?';
$pdfPage->drawText($unicodeString, 72, 720, 'UTF-8');

$pdfDoc->pages[] = $pdfPage;
$pdfDoc->save('utf8.pdf');

See also this bug log: http://framework.zend.com/issues/browse/ZF-3649


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...