1

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(i.e. 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): enter image description here

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

enter image description here 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:

enter image description here

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

Zakir_SZH
  • 466
  • 7
  • 21
  • no body is familiar with tcPDF unicode issue? – Zakir_SZH Sep 07 '15 at 10:07
  • i am very badly stuck and really need some help here... – Zakir_SZH Sep 07 '15 at 13:32
  • It's not so much an issue with Unicode as TCPDF's inability to handle rendering of Brahmic scripts. If you're really set on using TCPDF... do you have ImageMagick with Pango support installed? – EPB Sep 07 '15 at 17:05

1 Answers1

2

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:

Bangla rendered correctly, I think

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.
Community
  • 1
  • 1
EPB
  • 3,939
  • 1
  • 24
  • 26
  • dear sir, thanks a lot for your time and post/answer.. 1. sir, my project is already done (PDF part mainly) with tcPDF with some complex part like repeat column header on each page, auto page break etc? do you think it will not take long time to convert those code for mPDF? 2. yeah i also think write text on image then add on pdf is not the best way to go .. THANKS AGAIN FOR YOUR REPLY – Zakir_SZH Sep 08 '15 at 03:05
  • some how i maged to get to work with mPDF. But can't fine how to auto size text to fit it in a table cell . tcPDF has spaceing function does mPDF? – Zakir_SZH Sep 08 '15 at 13:56
  • sir, also mPDF is too (a lot) slower than tcPDF, same report mPDF took 564.4080 seconds where tcPDF took only 57.5938 seconds :( any idea? – Zakir_SZH Sep 08 '15 at 14:42
  • Regarding text sizing, I don't have a copy of mPDF available on this machine to test, but according to the second post at: http://www.mpdf1.com/forum/discussion/1476/autoresize-text-to-fit-into-a-div-width-height/p1 You can do it by adding a fixed-position block with `overflow:auto` and a constrained size. As for speed, are you using an opcode cache like eAccelerator, XCache, or APC? If not, see if you can install one. – EPB Sep 08 '15 at 19:38
  • Oh, and some more speed tips are available here: http://mpdf1.com/manual/index.php?tid=266 – EPB Sep 08 '15 at 19:45
  • thanks again for your reply. sir, i have tried xChace and also the steps on mpdd link to speedup, but nothing.. it take same time as before. any other way than mPDF? can pdflib lite (7) free version can do the job? – Zakir_SZH Sep 10 '15 at 06:58