How to use special characters in FPDF
FPDF is a nice little library for php that allows you to create PDFs on the fly. It’s great for all sorts of document, amongs them invoices. Until you try to use say a pound (£) or euro (€) sign. Basically the library itself does not support utf-8, so the moment you need to insert anything extra into your document a conversion needs to be done.
To save you some searching and a masive panic attack here is the easy way to do it:
iconv("UTF-8", "ISO-8859-1", "£");
You could also use the same command to do the whole text rather than just the pound sign itself. Example below:
You can use the iconv function at any point, however probably the easiest solution is to put it inside functions of the class you use to extend FPDF:
class myPdf extends FPDF {
[...]
public function showMoney($money){
$this->cell(10,10,iconv("UTF-8", "ISO-8859-1", "£"). $money,0);
}
}
You can leave a response, or trackback from your own site.


at 9:30 am
Having a few problems with FPDF and special characters myself (such as styled apostrophes and en rules) - can you shed a little more light on how to implement this fix - does it go in the fpdf.php or on the creation page?
Many thanks in advance!
W
at 10:45 am
Hi Wayne,
I’ve added an example. Basically you do not need to edit the library itself, you just convert the data before passing it on to FPDF.
Jo
at 12:52 am
[...] Think-Robot » Blog Archive » How to use special characters in FPDF [...]
at 4:00 am
Wow.. you saved me a million hours of work… I was looking into other ways to convert the UTF-8 symbols, and there are a few other fpdf libraries out there, but this hack is dead simple and works perfect! Thanks for putting up this post!
at 11:17 am
Awesome! Thanks so much for this!!!!!
at 6:33 pm
Hello,
for legacy applications, i wrote a small FPDF wrapper. This wrapper will replace the Cell method to include the iconv automatically.
class PDF extends FPDF {
// HACK: hack to fix another hack
function Cell($a, $b, $c, $d, $e, $f) {
return parent::Cell($a, $b, iconv(’UTF-8′, ‘ISO-8859-1′, $c), $d, $e, $f);
}
}
at 9:08 pm
You rock!
Thanks for the info! This helped me squash out a but quickly!
at 8:35 am
Hi,
I solved the special characters (àéè) with utf8_decode() …
Regards
at 2:55 am
You’re a star, saved me loosing what little hair I have left
at 8:02 pm
Thanks for your comment, cwhisperer. It helped me out of a maze.
at 8:16 pm
I was wondering how to properly display swedish characters and using “cwhisperer’s” idea was perfect. ie.
utf8_decode($html);
Thanks a million!