RSS - Latest posts How to use special characters in FPDF

Home » 2008 » 09 » 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.

Add Comment

  1. Wayne says:

    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

  2. Jo says:

    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

  3. [...] Think-Robot » Blog Archive » How to use special characters in FPDF [...]

  4. Joe Elliott says:

    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!

  5. Colin says:

    Awesome! Thanks so much for this!!!!!

  6. Marcos Álvares says:

    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);
    }
    }

  7. Max S. says:

    You rock!

    Thanks for the info! This helped me squash out a but quickly!

  8. cwhisperer says:

    Hi,
    I solved the special characters (àéè) with utf8_decode() …
    Regards

  9. Steve says:

    You’re a star, saved me loosing what little hair I have left :D

  10. Tim says:

    Thanks for your comment, cwhisperer. It helped me out of a maze.

  11. Victor says:

    I was wondering how to properly display swedish characters and using “cwhisperer’s” idea was perfect. ie.
    utf8_decode($html);

    Thanks a million!

  12. Raito says:

    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

  13. nejck says:

    Sadly none of the above solutions work for me with letters šđčćž :(

    any ideas?

  14. Using FPDF Version: 1.6 this solution required the windows-1252 character set. I.e.
    $str = iconv(‘UTF-8′, ‘windows-1252′, $str);

  15. 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

  16. Cyril says:

    Thank you very much for this tip. Simply hacked the cell function, as Marco suggested, it solved all my problems !

  17. Ben Hutchings says:

    You… rock.

  18. Neelima says:

    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”);

  19. Neelima says:

    I just resolved my issue .

    used html_entity_decode() , and now my pdf is generating perfectly. :D

  20. Cristian Göhl says:

    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);
    }