To faced this issue and I had to use dejavusans
font which is a UTF-8 Unicode font. This help fix the encoding issue when render UTF-8 words like ąčęėįšųūž
.
However, I was using helvetica
font which is standard ASCII font, and changing document's font to dejavusans
make it ugly.
To fix this I set two fonts to be loaded in the document and specified a part of the document to use dejavusans
font
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
/*
......
set margins and other options
......
*/
// Set font
// dejavusans is a UTF-8 Unicode font,
// if you only need to print standard ASCII chars,
// you can use core fonts like helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 12, '', true);
$pdf->SetFont('helvetica', '', 12, '', true);
$html = '<div style="font-family: helvetica;">' . $clientName . '</div><div style="font-family: dejavusans;">' . $clientAddress . '</div>';
// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$pdf->Output('invoice.pdf', 'I');