7

Can someone put together a clear and concise example of how you can create a PDF using TCPDF that will support text strings from any language?

It appears there is not a single font that will support all languages. I'm guessing the font would be too large?

I assume the correct way would be to detect the language of the string and dynamically set the font type to a compatible font. If this is the case then it gets very complex in detecting the language for each string.

Most languages are supported if you use the "freeserif" font. However it does not support CJK fonts. I've tried many fonts (kozminproregular, cid0jp, cid0kr, cid0jp, stsongstdlight) to get support for Chinese, Japanse, and Korean, but none of them seem to support all three languages.

Matt
  • 925
  • 2
  • 9
  • 19

5 Answers5

1

This worked out perfectly for me. Thank you!

To make sure, the generated PDF file will not get to big, use FontSubsetting - I have a 10 page PDF generated with only a few Lines of chinese (Names on Diplomas)

$pdf->setFontSubsetting(true); => PDF File slightly bigger 925kb vs 755kb without the chinese names if you use $pdf->setFontSubsetting(false); => PDF File size as about 17.5 MB ...

Christian K.
  • 528
  • 6
  • 16
1

In my application, I have to support almost 50+ languages. While generating the PDF all languages are not properly represented in pdf.

Solution

  1. Download and Add new Fonts (arialuni.tff) in to the tcpdf/fonts/ folder using the addTTfont method. It will generate the php, ctg.z, z files. Once the files are generated, we can remove the tff file from the fonts folder.

    TCPDF_FONTS::addTTFfont('path/to/folder'.'/lib/tcpdf/fonts/arialuni.ttf',
       'TrueTypeUnicode', '', 96); 
    
  2. Identify the country code and change the font type accordingly. Most of the languages will support freeserif. CJK languages will support arialuni.

    switch($iomadcertificate->lang){ 
        case 'my': 
        case 'az': $font = 'zawgyi'; 
            break; 
        case 'ja': 
        case 'ko': $font = 'arialuni'; 
            break; 
        case 'zhtw': $font = 'stsongstdlight'; 
            break; 
        default : $font = 'freeserif'; 
            break; 
    }
    $pdf->setFont($font, $style, $size);
    
ahuemmer
  • 1,653
  • 9
  • 22
  • 29
1

Managed this problem by making my own font from arial ms unicode with these steps:

In a temporal script put and execute this
1. put a copy of ARIALUNI.ttf in fonts folder under tcpdf installation (i've taken my copy from windows\fonts folder.
2. make a temporary script in examples folder of tcpdf and execute it with this line:
$fontname = $pdf->addTTFfont('../fonts/ARIALUNI.ttf', 'TrueTypeUnicode', '', 32);
3. set the new font in your pdf generator script:
$pdf->SetFont('arialuni', '', 20);

Now the pdf should be showing correctly CJK characters.
Hope this helps so many people.

Etiennez0r
  • 11
  • 2
  • How big is your font after doing this? Does it support chinese, japanese, korean? thanks. – Matt Sep 20 '12 at 04:19
  • It was bigger as i can remember, but it suported chinese, japanese and korean characters (CJK) as Matt asked. Not tried hindi unicode yet – Etiennez0r Oct 31 '13 at 23:07
0

I just tried Etiennez0r's solution, and it didn't work for me. Needed to make a minor modification as below:

$fontname = TCPDF_FONTS::addTTFfont('../fonts/ARIALUNI.TTF', 'TrueTypeUnicode', '', 96);
Chewpers
  • 2,430
  • 5
  • 23
  • 30
user5629388
  • 51
  • 1
  • 1
0

I setting:

$fontname = TCPDF_FONTS::addTTFfont(FCPATH . 'TCPDF/fonts/ARIALUNI.ttf', 'TrueTypeUnicode', '', 32);

....... // set font

$pdf->SetFont('dejavusans', '', 14);
$pdf->SetFont('cid0cs', '', 14);

Export Japanese is working well

Bang Andre
  • 471
  • 7
  • 11