How to write a PDF from PHP with a custom font and encoding


Update 01 Feb 2008

We just tried out TCPDF for a project requiring Thai, it works out of the box with the FreeSerif font for instance, great but bold Thai ends up as square boxes. I tried encoding Angsana New Bold (ANGSAB.TTF) with the new utility found in the tcpdf/fonts/ttf2ufm folder and the makefontuni.php script in the same folder, to no avail. I tried to use the old way described below, it didn’t work either. I even tried ttf2ufm.exe -b -L cp874.map angsab.ttf but that didn’t work either. So at the moment we’ve resorted to drawing all bold text twice in exactly the same place, this will give a bold effect but it doesn’t exactly feel right.

However, I didn’t try compiling the ttf2ufm utility on Linux and using that version, I can’t see how it would make a difference though (but you never know…). The problem is probably somewhere else, the *.ufm file looks ok, if you open it in a text editor all the Thai characters are there so the problem is somewhere else, the $cw array in angsab.php is empty however. If you take a look at the freeserif.php file it is shock full, doing the proper remapping there might make things work but this should’ve been done by the makefontuni.php script!

—————————–

With ttf2pt1 it is possible to convert true type fonts that abound in the Windows\Fonts directory to Type 1 fonts which can be used with a variety of PDF solutions. Converting with strange languages requires you to have a custom mapping that basically maps positions from a larger Unicode table to an ASCII table. In the process you will lose a lot of special characters that have to be thrown out to accommodate all the characters in the language of your choice (Thai for me). Some mappings can be found here. And here is an example of how such a map has to be formatted to work with ttf2pt1. Once you have a correct map setup you can convert your chosen TTF font like this:

ttf2pt1 -b -L cp874.map angsa.ttf angsa

In the above example the cp874 map is used which works for Thai characters. Make note of where you can find the resultant .afm and .pfb files. You will need them later (when you complete tutorial 7 on the fpdf site).

My favourite pdf solution up until now has been rospdf with all the nice ez functions. However when I converted the angsanaUPC font which is very popular among Thais I ran into trouble trying to use it with rospdf. If you have had better success than me don’t hesitate to comment on this post. So what are the alternatives? After a little bit of research into the Zend Pdf class I realized that I would not find any relief there either. Although the Zend Pdf class is very nice in that it can open pdfs and add content to them which could be extremely handy when filling in pdf forms from for instance an html form.

After completing tutorial 7 on the fpdf homepage I finally managed to get it to work by running this code with fpdf:

require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->AddFont('angsau','','angsau.php');
$pdf->SetFont('angsau','',16);
$text = "นพรสา่กสดา";
$text = iconv("UTF-8","cp874",$text);
$pdf->Cell(40,10,$text);
$pdf->Output();

However the fpdf scripts are not at all as nice as the ez stuff that rospdf has but fear not! Tcpdf to the rescue. You can easily use the files you got from completing tutorial 7 above with this extension to the fpdf library which seems to have a lot of the niceities of rospdf (haven’t tested much yet but the claims are there). Happy pdfing!

Related Posts

  • No Related Post

Tags: , , , , , , ,