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.

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
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
[...] Think-Robot » Blog Archive » How to use special characters in FPDF [...]
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!
Awesome! Thanks so much for this!!!!!
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);
}
}
You rock!
Thanks for the info! This helped me squash out a but quickly!
Hi,
I solved the special characters (àéè) with utf8_decode() …
Regards
You’re a star, saved me loosing what little hair I have left
Thanks for your comment, cwhisperer. It helped me out of a maze.
I was wondering how to properly display swedish characters and using “cwhisperer’s” idea was perfect. ie.
utf8_decode($html);
Thanks a million!
Hello!
I cant get baltic (ā ē ū ļ ņ š ķ ) font in fpdf invoice … how to decode to utf-8 ?!
Please help!
P.S.
What i need to do to convert the data before passing it on to FPDF ?
Best regards
Raitis
Sadly none of the above solutions work for me with letters šđčćž
any ideas?
Using FPDF Version: 1.6 this solution required the windows-1252 character set. I.e.
$str = iconv(‘UTF-8′, ‘windows-1252′, $str);
Excellent post! Had just found the issue when trying to display a pound sign in a PDF document, Googled and found this article!
Many thanks for sharing – has no doubt saved me loads of time
Here’s how I used it which worked 1st time!
$pdf->Write(5,’UNIT COST(‘.iconv(“UTF-8″, “ISO-8859-1″, “£”).’)');
Thanks
Richard
Thank you very much for this tip. Simply hacked the cell function, as Marco suggested, it solved all my problems !
You… rock.
Hi ,
I am facing the same issue special characters showing their code value in pdf.
I tried the solution you have mentioned here but it is not working for me .
Code :
$projDescr=utf8_decode($projDescr);
$pdf->MultiCell(100,5,iconv(”UTF-8″, “ISO-8859-1″, $projDescr),”0″,”J”);
I just resolved my issue .
used html_entity_decode() , and now my pdf is generating perfectly.
example:
function codificacao($string) {
return mb_detect_encoding($string.’x', ‘UTF-8, ISO-8859-1′);
}
function Cell($w, $h=0, $txt=”, $border=0, $ln=0, $align=”, $fill=false, $link=”)
{
if ($this->codificacao($txt) == ‘UTF-8′)
{
$txt = iconv(“UTF-8″, “ISO-8859-1″, $txt);
}