TCPDF UTF-8. Lithuanian symbols not showing up

前端 未结 15 655
-上瘾入骨i
-上瘾入骨i 2020-12-01 06:47

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 li

相关标签:
15条回答
  • 2020-12-01 06:52

    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.

    0 讨论(0)
  • 2020-12-01 06:52

    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

    0 讨论(0)
  • 2020-12-01 06:56

    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.

    0 讨论(0)
  • 2020-12-01 06:57

    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 :)

    0 讨论(0)
  • 2020-12-01 07:05

    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..

    0 讨论(0)
  • 2020-12-01 07:05

    Set font to freeserif it will work. I tested.

    $pdf->SetFont('freeserif', '', 14, '', true);
    
    0 讨论(0)
提交回复
热议问题