TCPDF – show UTF-8 characters in pdf

TCPDF is PHP class (or library) for generating PDF files. It’s great but sometimes you can encounter issue with UTF-8 encoding – special characters as Latvian, Russian, Lithuanian etc are displayed as ‘?’ or empty space. How to solve it:

1. Download tcpdf fromĀ  http://sourceforge.net/projects/tcpdf/
2. Upload the fonts files named ‘freesans’ (freesans.php, freesans.z and freesans.ctg.z) that are included in the Fonts folder of the files you downloaded andĀ  upload them to the /libraries/tcpdf/fonts
3. Open /libraries/joomla/document/pdf/fonts.php and find the line where $pdf->SetFont is written (about line 221)
4. Replace the line with something like that $pdf->SetFont(‘freesans’, ”, 6, ”, ‘false’);

If your website/application has panel, where you can change PDF parameters, go there and define font as freesans. Otherwise follow these steps:

5. Find “var $_header_font” and “var $_footer_font” (about lines 32 and 33) and replace the fonts there with freesans font:
var $_header_font = ‘freesans’;
var $_footer_font = ‘freesans’;

Also make sure that in file what includes tcpdf, charset is defined as utf-8:

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