Bangla (Unicode) font not rendering correctly in tcpdf

I am facing a problem with generating pdf in php by using TCPDF library. I need to show the bangla font correctly. I tried to add some bangla font(ie SolaimanLipi.ttf, SutonnyOMJ.ttf, Siyamrupali.ttf, Nikosh.ttf and so on). I can see the bangla font on pdf but the font is not display correctly. Its misplaced the word.

By adding this font I see the on the /fonts/ directory there successfully created 3 file “solaimanlipi.ctg.z”,”solaimanlipi.php” and “solaimanlipi.z”. As well as I can see the bangla font on pdf, but this font is misplaced/scattered. I am attaching a picture what I actually see.

This is how it should look (From browser screenshot):

Here is the code to show above result:

<?php
$strData = file_get_contents('./data3.txt');
?>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8" />
    <style>
    @font-face
    {
        font-family: myUniFont;
        src: url(./SolaimanLipi_22-02-2012.ttf);
    }
    </style>
</head>
<body>
    <span style="font-family: myUniFont;"><?php echo $strData; ?></span>
</body>
</html>

I use below code to use that very same font in my pdf:

$strBNFont = TCPDF_FONTS::addTTFfont('./SolaimanLipi_22-02-2012.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($strBNFont, '', 8, '', 'false');

And It is how it look like in PDF: :(

在这里输入图像描述 Please advice me how can I display the bangla font correctly.

EDIT #1

wow! ;( OMG!

dear sir, problem is not on pdf/tcPDF library nor even in the font file it self.

please check the below php code:

<?php
header('Content-type: image/png');

$text = 'তোমাদের  জন্য মুক্তিযুদ্ধের গল্প';

$font = './Fonts/SolaimanLipi_22-02-2012.ttf';

$im = imagecreatetruecolor(600, 300);

$bg_color = imagecolorallocate($im, 255, 255, 255);

$font_color = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 599, 299, $bg_color);

imagettftext($im, 20, 0, 10, 50, $font_color, $font, $font);

imagettftext($im, 20, 0, 10, 120, $font_color, $font, $text);

imagepng($im);

imagedestroy($im);

?>

this how it export/render the png image on browser:

在这里输入图像描述

when i try to print the text on image file using imagettftext function it also broke the character :(

as i am sure it's not font issue because i have just tested with 60+ fonts and all get broken.. while browser (html code) shows them very correctly.

so, i thing this is very bigger than my brain can contain/handle ;(

so, expert like you may only way out :)

thanks again for your times


TCPDF by itself can't handle Brahmic scripts.

I've posted similar for other languages: How can I create Malayalam PDF using TCPDF in PHP?

I believe mPDF has support for your text based on this example file: http://mpdf1.com/examples/example_utf8.pdf

I would suggest trying mPDF out if you're not dead-set on TCPDF. It's definitely easier if you can get it to work than the method I'm about to outline.


Another alternative, though, in my opinion far more complicated is to use ImageMagick with Pango to render your text as images and then include it in the PDF. This is different from ImageMagick's normal font rendering which as you saw is just as broken for your use. I'm including this more out of academic interest, I wouldn't necessarily suggest doing this unless you find a compelling reason to do so.

I basically had to do this from the shell after installing ImageMagick with Pango support:

#Install font for my user.
cp /host/SolaimanLipi_22-02-2012.ttf .fonts

#update the font-config cache
fc-cache

#Render the text with pango
convert -background white -size 400x pango:@/host/bangali.txt  /host/out.gif

Where /host/bangali.txt contained <span font='18.5'>তোমাদের জন্য মুক্তিযুদ্ধের গল্প</span> *

Which then renders output like this, which I think is at least mostly correct:

This is because Pango's shaping engine is capable of doing so. There are a number of caveats though to do it this way. Not the least of which is getting font-config to behave properly from CGI or mod_php, which is doable, but tricky in my experience.

  • I didn't have to specify a font name since I only have one Bangala font, so font-config found and used the one I had installed.
  • 链接地址: http://www.djcxy.com/p/88680.html

    上一篇: 英文字符与TCPDF

    下一篇: Bangla(Unicode)字体在tcpdf中不能正确显示