Creating PDFs using TCPDF that supports all languages especially CJK
Asked Answered
H

5

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.

Honeyman answered 24/2, 2012 at 7:30 Comment(3)
Have you got any proper solution? I'm also facing same issue.Outdo
@Outdo Apparently EtiennezOr's solution should work, but I have not completed the steps yet.Honeyman
$pdf->setFontSubsetting(true); $pdf->SetFont('cid0kr', '', 9); Worked for me on Korean characters on TCPDF 5.9Gripper
O
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.

Operational answered 17/3, 2012 at 0:49 Comment(2)
How big is your font after doing this? Does it support chinese, japanese, korean? thanks.Honeyman
It was bigger as i can remember, but it suported chinese, japanese and korean characters (CJK) as Matt asked. Not tried hindi unicode yetOperational
U
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 ...

Uropod answered 17/6, 2014 at 11:9 Comment(0)
M
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);
    
Mechanism answered 24/8, 2023 at 14:10 Comment(0)
S
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);
Shaun answered 1/2, 2016 at 19:23 Comment(0)
M
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

Misbeliever answered 3/10, 2017 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.