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:
    12th December 2008
    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

  2. Jo says:
    12th December 2008
    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

  3. 22nd February 2009
    at 12:52 am

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

  4. Joe Elliott says:
    21st October 2009
    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!

  5. Colin says:
    4th November 2009
    at 11:17 am

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

  6. Marcos Álvares says:
    10th December 2009
    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);
    }
    }

  7. Max S. says:
    29th January 2010
    at 9:08 pm

    You rock!

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

  8. cwhisperer says:
    12th February 2010
    at 8:35 am

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

  9. Steve says:
    27th February 2010
    at 2:55 am

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

  10. Tim says:
    30th April 2010
    at 8:02 pm

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

  11. Victor says:
    5th June 2010
    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!