孟加拉语(Unicode)字体在tcpdf中无法正确呈现 [英] Bangla (Unicode) font not rendering correctly in tcpdf

查看:184
本文介绍了孟加拉语(Unicode)字体在tcpdf中无法正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临使用TCPDF库在php中生成pdf的问题.我需要正确显示孟加拉字体.我尝试添加一些孟加拉字体(即SolaimanLipi.ttf,SutonnyOMJ.ttf,Siyamrupali.ttf,Nikosh.ttf等).我可以在pdf上看到bangla字体,但是该字体显示不正确.它放错了单词.

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.

通过添加此字体,我在/fonts/目录中成功创建了3个文件"solaimanlipi.ctg.z","solaimanlipi.php"和"solaimanlipi.z".我可以在pdf上看到bangla字体,但是这种字体放错了位置.我要附上一张我实际看到的照片.

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>

我使用以下代码在pdf中使用相同的字体:

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

这就是PDF中的样子::(

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

请告诉我如何正确显示孟加拉字体.

Please advice me how can I display the bangla font correctly.

编辑#1

哇! ;(OMG!

亲爱的先生,问题不在pdf/tcPDF库上,甚至不在它自己的字体文件中.

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

请检查以下php代码:

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

?>

这是如何在浏览器中导出/渲染png图像的方法:

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

当我尝试使用imagettftext函数在图像文件上打印文本时,它也会破坏字符:(

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

我确定这不是字体问题,因为我刚刚测试了60多种字体,并且所有字体都损坏了..而浏览器(html代码)可以非常正确地显示它们.

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

再次感谢您的光临

推荐答案

TCPDF本身我已经为其他语言发布了类似的文章:

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

我相信mPDF支持基于此示例文件的文本: http://mpdf1.com /examples/example_utf8.pdf

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

我建议您尝试 mPDF TCPDF.如果您可以使用它,那肯定比我将要概述的方法要容易得多.

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.

不过,在我看来,更为复杂的另一种选择是将ImageMagick与Pango一起使用,将您的文本呈现为图像,然后将其包含在PDF中.这与ImageMagick的常规字体渲染不同,正如您所看到的那样,该字体在使用时也同样损坏.我之所以将其包括在内是出于学术上的兴趣,除非您有充分的理由这样做,否则我不一定建议您这样做.

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.

安装具有Pango支持的ImageMagick之后,我基本上必须从外壳执行此操作:

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

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

然后呈现这样的输出,我认为至少在大多数情况下是正确的:

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

这是因为Pango的整形引擎能够做到这一点.尽管这样做有很多注意事项.并非最不重要的是,可以通过CGI或mod_php使font-config正常运行,这是可行的,但根据我的经验是棘手的.

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.

  • 我不必指定字体名称,因为我只有一种Bangala字体,所以font-config找到并使用了我已经安装的字体.
  • 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.

这篇关于孟加拉语(Unicode)字体在tcpdf中无法正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆