TCPDF 6.0.047 - UTF-8 and time new roman font
我尝试使用tcpdf进行报告,但不适用于utf-8(越南)
1 2 3 4 5 6 7 8 | // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetFont('times', '', 10, '', false); $pdf->writeHTML("T?NG H?P", true, false, false, false, ''); //Close and output PDF document $pdf->Output('test.pdf', 'I'); |
但是输出是
1)字体
首先,我将c:\\\\ windows \\\\ fonts的Windows8" Times new roman"字体复制到名为" tnr"的tcpdf / fonts下的临时目录。有4个文件" times.ttf"," timesbi.ttf"," timesbd.ttf"," timesi.ttf"。
2)然后从tcpdf / fonts
中删除了" times.php"
3)重制times字体def并为此添加新的bi(粗体斜体)
1 2 3 4 5 6 7 8 9 | // remake times $fontpath1='../fonts/tnr/times.ttf'; $fontpath2='../fonts/tnr/timesbd.ttf'; $fontpath3='../fonts/tnr/timesbi.ttf'; $fontpath4='../fonts/tnr/timessi.ttf'; $fontname1 = $pdf->addTTFfont($fontpath1, 'TrueTypeUnicode', '', 96); $fontname2 = $pdf->addTTFfont($fontpath2, 'TrueTypeUnicode', '', 96); $fontname3 = $pdf->addTTFfont($fontpath3, 'TrueTypeUnicode', '', 96); $fontname4 = $pdf->addTTFfont($fontpath4, 'TrueTypeUnicode', '', 96); |
4)修改了脚本,以全部4种方式打印字符串。粗体和斜体不起作用,但不确定如何重新制作它们。但是,普通的和粗体的斜体字确实有效,您可以看到对比度。
5)修改后的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php // Include the main TCPDF library (search for installation path). require_once('tcpdf_include.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator('zzz'); $pdf->SetAuthor('zzz'); // set default font subsetting mode //$pdf->setFontSubsetting(true); // remake times $fontpath1='../fonts/tnr/times.ttf'; $fontpath2='../fonts/tnr/timesbd.ttf'; $fontpath3='../fonts/tnr/timesbi.ttf'; $fontpath4='../fonts/tnr/timessi.ttf'; $fontname1 = $pdf->addTTFfont($fontpath1, 'TrueTypeUnicode', '', 96); $fontname2 = $pdf->addTTFfont($fontpath2, 'TrueTypeUnicode', '', 96); $fontname3 = $pdf->addTTFfont($fontpath3, 'TrueTypeUnicode', '', 96); $fontname4 = $pdf->addTTFfont($fontpath4, 'TrueTypeUnicode', '', 96); $pdf->AddPage(); $string="T?NG H?P"; $pdf->SetFont('times', '', 14, '', false); $pdf->writeHTML('default ' . $string,true, 0, true, 0); $pdf->SetFont('times', 'b', 14, '', false); $pdf->writeHTML('bold ' . $string,true, 0, true, 0); $pdf->SetFont('times', 'i', 14, '', false); $pdf->writeHTML('italic ' . $string,true, 0, true, 0); $pdf->SetFont('times', 'bi', 14, '', false); $pdf->writeHTML('bold italic ' . $string,true, 0, true, 0); //Close and output PDF document $pdf->Output('test.pdf', 'I'); ?> |
我在波兰语字符上也遇到了类似的问题,但仅在Firefox PDF浏览器中。
尝试一下:
1 | $pdf->setFontSubsetting(false); |
编辑
我检查了您的示例数据。setFontSubsetting不起作用(如您所说):(
我做了一些测试。当我更改字体报告后,便开始正常工作。
例如:
1 | $pdf->SetFont('freeserif', '', 10, '', false); |
或
1 | $pdf->SetFont('dejavuserifbi', '', 10, '', false); |
和其他字体:freesans,dejavuserifi,freeserif,但不是" times"。
正确使用定义中的所有字体:
1 | $type='TrueTypeUnicode'; |
(在tcpdf / fonts / freesans.php中查看)
我认为'times'没有用于utf8编码的完整字符表。在字体定义中,只有255个元素,但在" freeserif"中则超过65k。对于标准ASCII字符,"时间"只是"核心字体"。
您是否尝试过'times'?:
使用支持所有语言(尤其是CJK
)的TCPDF创建PDF
编辑
我从上面的链接测试了解决方案。.
我的步骤:
在同一目录中,从命令行运行:
1 | ttf2ufm -a -F times.ttf |
和
1 | php -q makefont.php times.ttf times.ufm |
注意:此工具在TCPDF的"最小"版本中不可用。
运行脚本:
1 2 3 4 5 6 7 | // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetFont('times', '', 10); $pdf->AddPage(); $pdf->writeHTML("T?NG H?P", true, false, false, false, ''); $pdf->Output('test.pdf', 'I'); |
这对我非常有用:)
我也遇到了这个问题。我使它工作的方法是使用dejavusans字体,因为该字体是为utf8 unicode设计的,而tcpdf已经拥有它。
演示代码:
1 | $pdf->SetFont('dejavusans', '', 14, '', true); |