Matt
Matt

Reputation: 935

Creating PDFs using TCPDF that supports all languages especially CJK

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.

Upvotes: 7

Views: 11290

Answers (5)

keerthy joseph
keerthy joseph

Reputation: 11

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

Upvotes: 1

Bang Andre
Bang Andre

Reputation: 541

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

Upvotes: 0

user5629388
user5629388

Reputation: 51

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

Upvotes: 0

Christian K.
Christian K.

Reputation: 538

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

Upvotes: 1

Etiennez0r
Etiennez0r

Reputation: 11

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.

Upvotes: 1

Related Questions