27

Im using latest TCPDF version(5.9). But have some strange problems with encoding. I need Lithuanian language symbols like: ąčęėįšųūž. But get only few of it. Other remain like ????? So what should I do ? I use default times font(it comes with TCPDF download).

Any help would be appreciated.

miken32
  • 42,008
  • 16
  • 111
  • 154
Bounce
  • 2,066
  • 6
  • 34
  • 65
  • The encoding of generated PDF document is ANSI. Such information shows PDF reader. But how can it be ? I defined in my code, that encoding is UTF-8... – Bounce Mar 17 '11 at 01:11

16 Answers16

33

TCPDF is quite tricky with utf8. Best way to achieve what you want is to embed the font in generated PDF file itself. You can use freeserif font from the TCPDF package, it contains all the utf8 symbols, shows absolutely any character of any language, but adds ~700kb to the output file. That's probably the easiest way to get symbols you need if file size doesn't matter.

You could also make your own font to embed, containing the characters you need. That's probably the best solution, keeping it universal and small in size, but is more complex.

Alternatively, you can relay on core fonts, which are taken from the system, and if not found, replaced by a substitute. This makes output file extremely light, but adds the necessity of font subsetting to obtain exotic chars. Personally I haven't had a success with this, so I still think embedding font is the best solution, which also happens to be more universal..

squirrely
  • 439
  • 1
  • 4
  • 3
  • 1
    Awesome tip with freesans font, I´d upvote you 10 times, saved me a lot of work! – Max Sep 21 '11 at 14:02
  • 6
    Freesans also helped me. Thanks a lot! Also, I want to add that the 'freesans' font should be set both in PHP ($pdf->SetFont('freesans')) and in HTML, if you try to print tables, like I did. Just add the 'style' attribute like this: ... Helped me to display some Czech characters. – parrker9 May 08 '12 at 10:26
  • "Best way to achieve what you want is to embed the font in generated PDF file itself." - how can i achieve that? – emfi Jan 15 '16 at 14:18
  • @squirrely i love you, parrker9 i love you – ononononon Nov 24 '17 at 13:43
  • In my case, simply changing the font (to freesans) in the inline style of the original HTML (I am using writeHTML) did the trick. You need to have the font in the fonts folder, though. – Eugenio Apr 16 '18 at 07:45
16

there is a font included in the CPDF core fonts - dejavusans, it shows all the lithuanian characters. Just add the following:

$pdf->setHeaderFont(Array('dejavusans', '', 10, '', false));
$pdf->setFooterFont(Array('dejavusans', '', 8, '', false));
$pdf->SetFont('dejavusans', '', 10, '', false);
Laurynas Mališauskas
  • 1,909
  • 1
  • 19
  • 34
14

Set font to freeserif it will work. I tested.

$pdf->SetFont('freeserif', '', 14, '', true);
Truongnq
  • 367
  • 4
  • 7
  • can't believe took almost two hours to figure out – Shahbaz Jan 18 '18 at 17:24
  • Thank you I was trying to write Greek in the header/footer and only this answer helped me. I had to pass the characters through `htmlentities()`, and now the freeserif font displays them correctly. – alexg Aug 07 '19 at 16:37
14

Set the $unicode parameter on the TCPDF constructor to false and the $encoding parameter to 'ISO-8859-1' or some other character map.

This will help you:

Default for UTF-8 unicode:

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

Example of constructor for European charset:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
Elisa
  • 6,865
  • 11
  • 41
  • 56
  • 1
    Please mention the file that this line should be added/modifeid – Ata Iravani Nov 26 '13 at 06:38
  • 1
    this does absolutely not work, when the encoding of the text is acutally utf-8. in my case everything is more worse with setting parameters to >>false, 'ISO-8859-1'<<... – emfi Jan 15 '16 at 14:11
  • @emfi Dont forget to change the encoding of the source file itself from 'UTF-8' to 'ANSI' – ACs Jan 28 '16 at 15:49
10

Just discovered this same situation when trying to render Romanian text using the default Helvetica font. In doing some investigation I found that the tcpdf library treats it's default fonts (referred to as "core" fonts) as Latin1 characters so even if you tell it to use UTF-8 encoding and set the unicode flag, it will literally translate your text to Latin1 equivalents prior to rendering. The default behavior of the library is, if it finds a Latin1 equivalent, to translate each character that it can find an equivalent for otherwise it translates the character as '?'.

This can be found inside the TCPDF class in the following method chain: Write() -> Cell() -> getCellCode() -> _escapetext().

Inside of _escapetext() you can see it is checking for $this->isunicode then checking the selected font to see if it's type is core|TrueType|Type1. If it is, it will take the string an "latinize" it for you by way of the UTF8ToLatin1() method. This is where the '?' translations are taking place.

My recommendation would be to use a custom unicode font (like Deja Vu Sans) that is similar to the default font you are after. That worked for me in my current situation.

RobertGonzalez
  • 164
  • 1
  • 4
8

To use TCPDF with special characters like ฿, 포 or others you need to use a unicode font:

  1. download the font here: ftp://ftp.fu-berlin.de/unix/X11/multimedia/MPlayer/contrib/fonts/arialuni.ttf.bz2

  2. create a test pdf file and load this font into TCPDF example:

    $fontname = $pdf->addTTFfont('/var/www/app/images/fonts/arialuni.ttf', 'TrueTypeUnicode', '', 32);

  3. this will create the fonts like:

    application/libraries/tcpdf/fonts/arialuni.ctg.z
    application/libraries/tcpdf/fonts/arialuni.php
    application/libraries/tcpdf/fonts/arialuni.z

  4. now you can set the new font with : $pdf->SetFont('arialuni', '', 10.5);

  5. and now you can use special unicode characters like ฿ and more....

Source : http://myridia.com/dev_posts/view/852

Ledadu
  • 111
  • 1
  • 4
  • i am using version 6.2.12 and the method $pdf->addTTFfont() does not exist here? do you or someone else have any information? – emfi Jan 15 '16 at 14:27
  • @emfi, now "addTTFfont" is a static method. You can find the example here: https://github.com/tecnickcom/TCPDF/blob/7214fa2103abc25d244f76991358a386c614a170/tools/tcpdf_addfont.php#L250 – Alexander Reznikov Aug 12 '18 at 23:56
5

You u have problem to read character like Karnātaka from database and display like this karn?taka I mean "?" which we don't want then do following things :

  1. Define charset for the connection (mysql_set_charset()):

    $con = mysql_connect("localhost","root","");
    
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("database_name", $con) or die(mysql_error());
    mysql_set_charset('utf8',$con);
    
  2. Use $pdf->SetFont('DejaVuSerif', '', 10); instead of $pdf->SetFont('helvetica', 'B', 12);

    • For TCPDF Library of the PHP read character like Rājasthān instead of R?jasth?n from database
Taz
  • 3,718
  • 2
  • 37
  • 59
Aki
  • 51
  • 1
  • 1
3

With default TCPDF package tested dejavusans and freeserif and both fonts works with lithuanian characters. I also typed few russian characters and they worked too. I used this code to test it:

$this->pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
$this->pdf->AddPage();
$this->pdf->SetFont('dejavusans', 'B', 20); // UTF8 fonts with lithuanian support: freeserif, dejavusans
$this->pdf->Write(0, 'ąžūčšęėųįĄŽŪČŠĘĖŲĮ Превед Кросавчег!', '', 0, 'C', true, 0, false, false, 0);
3

IIRC, you can define an encoding when you create a new font, as described here. Otherwise, you have to use the encoding that was defined when the font was created. It sounds like the fonts that ship with TCPDF all use WinAnsiEncoding... a.k.a. code page 1252.

Clunky, but effective.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • I can confirm this is how I got it working for Romanian characters. I tried generating Arial (ttf) in several ways but ultimately failed. It worked the first time when I tried with MyriadPro (utf). – Alex Ciminian Apr 09 '11 at 19:49
2

With dejavusans font it worked fine for both Russian and Latvian letters.

JustAMartin
  • 13,165
  • 18
  • 99
  • 183
1

With me it was a font problem. I used the font timesand my local multibyte chras wouldn't show up properly. When I changed it to freeserif they were working normally :)

Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28
0
$fontname = $pdf->addTTFfont('C:\xampp\htdocs\copyshop\fonts\07-TH-Sarabun-PSK\THSarabun.ttf', 'TrueTypeUnicode', '', 32);
    $pdf->SetFont($fontname, '', 16,'',FALSE); //Working
Donot Don't
  • 609
  • 7
  • 12
0

I had the same issue with Romanian characters and the problem wasn't the encoding, LC_CTYPE or other setting from TCPDF, but the font I used. I mention that I used TWIG templating with Courier font. You can try to change your font to freeserif

0

change the font to show normally ₹ and Lithuanian symbols

$pdf->SetFont('cid0cs', '', 12);
Gajarajan K
  • 459
  • 4
  • 9
0

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');
Walid Ajaj
  • 518
  • 4
  • 8
0

For this use the following code of the parameter TCPDF constructor

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

It will help you.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Anil Kumar
  • 11
  • 2