1

我正在为此启动一个新线程,将尝试尽可能具体。

这是我们的销售人员应用程序的一部分,允许他们使用他们的联系信息自定义页面,在他们点击提交后,会创建一个 PDF 页面,然后将其附加到较大文档的末尾。

文档的第一页也是从他们填写的同一个表单动态创建的。在他们按下提交后,会创建 2 个 PDF 文件,一个首页和一个后页,然后将两个页面都附加到一个更大的文档中,从而创建一个报告。

除非他们输入汉字,否则一切都很好,那么我得到的只是像这样的垃圾字符 - (质釕俕试æμ•ç¨‹)。即使在与更大的文档合并之前,文档仍然看起来像这样。

我已经尝试了所有可能的场景(似乎),从使用 Unicode 字体,到使用我自己创建的字体,并逐个尝试 TCPDF 文件夹中的每种字体。我完全不知所措,而且已经有一段时间了。我觉得我几乎去过所有提到 TCPDF 的网站。

这是创建后页的代码,回到我开始使用的基本命令。我希望这是一个我缺少的简单命令:

require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
    $html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB)); 
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML($html, isset($_GET['vuehtml']));

//Line below saves PDF to file
    $html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');

}

catch(HTML2PDF_exception $e) {
    echo $e;

}

值得一提的是,运行 Centos 5.5 服务器来创建文档。先感谢您。


在用 HTML2PDF 对这个问题进行了一些调查后,我被告知字体 arialuni.ttf 应该支持我需要的所有字符。话虽如此,当我在下面看到的脚本中使用它时,HTML2PDF 将不会加载此字体。

require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{

    $html2pdf = new HTML2PDF('L','LETTER','en', false, '', array(mL, mT, mR, mB));      
    $html2pdf->pdf->AddTTFFont('ARIALUNI.TTF');
    $html2pdf->pdf->setFont('arialuni');
    $html2pdf->writeHTML($html, isset($_GET['vuehtml']));

    //Line below sends output to the screen only
    //$html2pdf->Output('exemple00.pdf');

    //Line below saves PDF to file
   $html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');

}

catch(HTML2PDF_exception $e) {
    echo $e;

}

*我能想到的只是语法不是 100% 正确的。我已经运行了 tt2fm 和 makefont 来获取文件,但不觉得这些例程是 100% 成功的。

请帮忙 - 我已经断断续续地研究这件事几个月了。我几乎准备把它报废然后敲门。

谢谢你。

4

4 回答 4

2

您可以使用以下方法在 PDF 文档中呈现中文字符

TCPDF 中的cid0jp字体。

供你参考 :-

PHP 代码:-

http://www.tcpdf.org/examples/example_038.phps

生成的PDF:-

http://www.tcpdf.org/examples/example_038.pdf

请确保文件cid0jp.php应该在字体目录中。

tcpdf/fonts/cid0jp.php 
于 2013-01-22T06:34:43.513 回答
2

根据您的代码:

require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
    $html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB));
    //$html2pdf->setDefaultFont('Arial');  //Please remove this
    $html2pdf->setDefaultFont('stsongstdlight'); //And change with this, it works 101% for me
    $html2pdf->writeHTML($html, isset($_GET['vuehtml']));

//Line below saves PDF to file
    $html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');

}

catch(HTML2PDF_exception $e) {
    echo $e;

}

...这是我 101% 的工作代码,在 pdf 输出中包含中文字符:

<?php
require_once ROOT_DIRECTORY.'/{it's your own location where you put the class at}/html2pdf-4.4.0/html2pdf.class.php'; //ROOT DIRECTORY is my own "defined" so do not use this line.
/* or use this:
* require_once(dirname(__FILE__).'/html2pdf.class.php');
* it depends on what you need
*/ 
try
{
    $html2pdf = new HTML2PDF('L', 'A6', 'en', true, 'UTF-8', 0); //I'm using UTF-8 //Also I'm working on A6 page in my project here, so if you need to have A4 size change A6 back to A4 or change to the size you need
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->setDefaultFont('stsongstdlight'); //this is all you need to have the CHINESE character.
    $content = ob_get_clean();
    $html2pdf->writeHTML($content);

    $html2pdf->Output('Sticker-'.$this->invNumber.'-'.$this->custCode.'.pdf');
    exit;
}
catch(HTML2PDF_exception $e) {
    echo $e;
    exit;
}

希望它在您的代码中也能正常工作!

快乐编码:)

于 2016-08-29T08:36:25.657 回答
0
$html2pdf = new HTML2PDF('L','LETTER','en', true, 'UTF-8', array(mL, mT, mR, mB));
于 2014-01-28T07:31:36.130 回答
0

所以html2pdf是基于 的tcpdf,但它们有点不同: html2pdf不支持中文。

但是,您可以javiergb.php从包含目录中获取/tcpdf/fonts/。然后将此文件复制到html2pdf/_tcpdf5.xxxx/fonts/目录中。

之后,您只需将该行添加$html2pdf->setDefaultFont('javiergb');到您的 html2pdf 设置中,如下所示:

$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->setDefaultFont('javiergb');
$html2pdf->writeHTML($html);
$html2pdf->Output('example_zh_cn.pdf');

你可以看看我的叉子:https ://github.com/cychai/html2pdf

于 2015-06-07T18:19:32.113 回答