TCPDF – HTML to PDF conversion, encoding problem for extended chars

In PHP on May 26th, 2011

TCPDF is a-little-great HTML to PDF conversion PHP library. One of main problem, I (and lot others) had encountered displaying extended characters (like European). To resolve this, you need set $unicode parameter false and pass $encoding parameter other character set. Most of given example comes with, $unicode=true and $encoding=’UTF-8′. Below different examples:

Default for UTF-8 unicode:

  1. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

Example of constructor for European charset:

  1. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);

Notice above, 4th and 5th params. 4th is $unicode and 5th is encoding params respectively

Example from TCPDF, for extended chars:

  1. PHP: http://www.tcpdf.org/examples/example_008.phps
  2. PDF: http://www.tcpdf.org/examples/example_008.pdf

Further reading:
http://www.tcpdf.org/performances.php (4th point)

  • Dutch dude

    Thank you very much. You saved my day.

  • Dutch dude

    Not quite!

    At first your solution seems correct, but I found out it isn’t. Well it wasn’t in my case.

    I did what you told and it worked, Extended characters are being printed correctly. But than I found out that TCPDF has replaced Tahoma, the true type font I selected, with it’s standard font Helvetica!

    I guess the reason is because Tahoma is an Unicode font and we told TCPDF to switch Unicode off. It can not use the font so it replaces the font by one of it’s standard fonts. Can anybody confirm this?   

    So, if I want to use a True Type font, I have to use UTF-8 and set UniCode to true!

    Example correct constructor:
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘UTF-8′, false);

    But hé, that does not work! Extended characters (like characters with accents) are not printed correctly.

    In my case, strings  were read from the database and were not in UTF-8 format. They were stored in the database in latin1_swedish_ci. To have TCPDF handle the strings correctly, I had to convert them to UTF-8.
    This was fairly simple. You can just use PHP-function utf8_encode().

    Now everything is printed correctly. I hope this helps somebody else.

  • Michael

    Hi have a converter for the converting from ttf and otf fonts for tcpdfhttp://fonts.snm-portal.com/