coding, hacking, startups, computer security, technology and more

Euro Symbols on Dynamically Generated PDFs With PHP

Recently it became necessary for me to display a euro symbol in a PDF which was being generated using PDFlib on PHP.  After some trials and tribulations between googling for solutions and experimenting with different techniques I finally figured out what the problem is.

Firstly, the euro symbol can be passed to PDFLib as chr(128) but this will only work if the font has been loaded using the correct encoding. There's two methods of using PDFLib, the non-OOP approach:

$font = pdf_findfont($pdf, "Helvetica-Bold", "host", 0);

and the OOP method:

$font = $objPDF->load_font("Helvetica", "host", "");

The problem with the above methods of loading the fonts is that the encoding is determined by the OS which in FreeBSD is something other than winansi. So the solution is simple, modify your font loading call accordingly, specifying winansi as the encoding.  For the non-OOP approach:

$font = pdf_findfont($pdf, "Helvetica-Bold", "winansi", 0);

or the OOP method:

$font = $objPDF->load_font("Helvetica", "winansi", "");

I hope somebody finds this useful - there's not much information on googles search index which explains how to do this!