Zend_Pdf - Cyrillic May 5, 2009

I’ve been working on a project which requires complete compatibility with UTF-8 and in particular with cyrillic characters. From the database to the PDF generator, there are little details to be aware of. The particular issue I want to address here is the problem of creating PDF files which contain cyrillic characters. It seems straight forward… just drop them in, make sure they’re UTF-8 encoded and hey presto! Naturally, its not that simple.

For this project, we’ve been using elements of the Zend Framework, which contains a large number of very useful and well developed classes. In particular, the file upload capabilities and the mail sending classes are very quick and easy to implement. I haven’t quite bought into using the framework’s MVC components… yet. I still prefer our in-house framework because it’s much lighter and pretty easy to work with. (I do appreciate MVC and all the other patterns, butI have always stopped short of actually calling my classes ‘ThisController’ or ‘ThatModel’, preferring instead, for the patterns to be unnamed and in the background. I like to concentrate on making my objects specific to the business problem at hand…) Anyhow, back to the issue. The Zend Framework has a fairly underdeveloped PDF class called Zend_Pdf. When I say underdeveloped, I really mean it doesn’t have many of the neat features that make things like wordwrapping easy. It is superbly simple to create a basic Pdf:


$pdf = new Zend_Pdf();
$pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$pdfPage = $pdf->pages[0];
$pdfPage->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 12);
$pdfPage->drawText(’This is my text.’, 72, 740, ‘UTF-8′
);
$pdf->save('/somehere/on/the/machine/document.pdf');

The Zend_Pdf class lets you choose your font from the standard list of 14  seemingly available to all PDF readers. This is where the problem lies. When you start dropping cyrillic into the document, the class starts having fits.

To solve this, you need to find a font which includes the cyrillic characters… simple! I looked around and found this collection. Some very nice free fonts. I downloaded freefont-ttf and dropped them into my app. With a small change in the code, substituting Zend_Pdf_Font::fontWithName() with Zend_Pdf_Font::fontWithPath() and using the path to the font as the latter’s argument, gets you a decent font complete with cyrillic characters:

$pdfPage->setFont(Zend_Pdf_Font::fontWithPath('/var/blah/dah/FreeSerif.ttf'), 12);

Having discussed the Zend_Pdf class, I have to mention that I’ll probably end up using TCPDF until the Zend_Pdf class comes around with a few more features… just an HTML to PDF function would be great.

There is one other issue here. Since the built-into-PDF fonts do not seem to include (am I wrong here?) any fonts with cyrillic characters, any external fonts you use, need to be embedded into the document, increasing it’s file size by quite a bit sometimes. A simple ‘Hello World’ in cyrillic comes in at a hefty 800K, thanks to the font. TCPDF includes the free fonts mentioned above in the download package. Well thought out.

Leave a Reply