Question

I try to make a report with tcpdf but it's not working with utf-8 (Vietnamese)

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetFont('times', '', 10, '', false);

$pdf->writeHTML("TỔNG HỢP", true, false, false, false, '');

//Close and output PDF document
$pdf->Output('test.pdf', 'I');

But output is T?NG H?P. How to fix that thanks

Was it helpful?

Solution

1) Fonts First, I copied my Windows8 "Times new roman" fonr from c:\windows\fonts to a temp dir under tcpdf/fonts called "tnr". There are 4 files "times.ttf", "timesbi.ttf", "timesbd.ttf", "timesi.ttf".

2) Then removed "times.php" from tcpdf/fonts

3) Remake the times font def and add the new bi (bold italic) with this

// remake times
$fontpath1='../fonts/tnr/times.ttf';
$fontpath2='../fonts/tnr/timesbd.ttf';
$fontpath3='../fonts/tnr/timesbi.ttf';
$fontpath4='../fonts/tnr/timessi.ttf';
$fontname1 = $pdf->addTTFfont($fontpath1, 'TrueTypeUnicode', '', 96);
$fontname2 = $pdf->addTTFfont($fontpath2, 'TrueTypeUnicode', '', 96);
$fontname3 = $pdf->addTTFfont($fontpath3, 'TrueTypeUnicode', '', 96);
$fontname4 = $pdf->addTTFfont($fontpath4, 'TrueTypeUnicode', '', 96);

4) modifed script to print the string all 4 ways. Bold and italic don't work, but not sure how to remake them. However, plain and bold italic do work, and you can see the contrast.

5) modified script


<?php
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator('zzz');
$pdf->SetAuthor('zzz');


// set default font subsetting mode
//$pdf->setFontSubsetting(true);

// remake times
$fontpath1='../fonts/tnr/times.ttf';
$fontpath2='../fonts/tnr/timesbd.ttf';
$fontpath3='../fonts/tnr/timesbi.ttf';
$fontpath4='../fonts/tnr/timessi.ttf';
$fontname1 = $pdf->addTTFfont($fontpath1, 'TrueTypeUnicode', '', 96);
$fontname2 = $pdf->addTTFfont($fontpath2, 'TrueTypeUnicode', '', 96);
$fontname3 = $pdf->addTTFfont($fontpath3, 'TrueTypeUnicode', '', 96);
$fontname4 = $pdf->addTTFfont($fontpath4, 'TrueTypeUnicode', '', 96);


$pdf->AddPage();
$string="TỔNG HỢP";

$pdf->SetFont('times', '', 14, '', false);
$pdf->writeHTML('default ' . $string,true, 0, true, 0);
$pdf->SetFont('times', 'b', 14, '', false);
$pdf->writeHTML('bold ' . $string,true, 0, true, 0);
$pdf->SetFont('times', 'i', 14, '', false);
$pdf->writeHTML('italic ' . $string,true, 0, true, 0);
$pdf->SetFont('times', 'bi', 14, '', false);
$pdf->writeHTML('bold italic ' . $string,true, 0, true, 0);


//Close and output PDF document
$pdf->Output('test.pdf', 'I');
?>

OTHER TIPS

I had a similar problem with Polish characters, but only with firefox pdf browser.

Try this:

$pdf->setFontSubsetting(false);

EDIT

I checked on Your example data.. setFontSubsetting not working (as You say) :(

I did a little tests. When I changed the font reports then began to work properly.

Ex:

    $pdf->SetFont('freeserif', '', 10, '', false);

or

    $pdf->SetFont('dejavuserifbi', '', 10, '', false);

and other fonts: freesans, dejavuserifi, freeserif, but not 'times'.

Working correctly with all fonts that have in definition:

    $type='TrueTypeUnicode';

(look in tcpdf/fonts/freesans.php)

I think that 'times' have not full character table for utf8 coding. In font definition, is only 255 elements, but in 'freeserif' is over 65k. 'Times' is only 'core font' for standard ASCII characters.

Did You try this for 'times' ?: Creating PDFs using TCPDF that supports all languages especially CJK

EDIT

I tested solution from above link..

My steps:

  1. Copy times.ttf from windows font directory to tcpdf/fonts/utils
  2. In same directory, run from command line:

    ttf2ufm -a -F times.ttf
    

and

    php -q makefont.php times.ttf times.ufm

Note: This tools are not available in 'min' version of TCPDF.

  1. Copy new files : times.ctg.z, times.php, times.t1a, times.z into tcpdf/fonts

Run script:

    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetFont('times', '', 10);
    $pdf->AddPage();

    $pdf->writeHTML("TỔNG HỢP", true, false, false, false, '');
    $pdf->Output('test.pdf', 'I');

This works great for me :)

I faced this issue also. And the way I made it work was using dejavusans font as the font is designed for utf8 unicode and tcpdf had it already.

Demo code:

$pdf->SetFont('dejavusans', '', 14, '', true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top