我正在使用TCPDF库版本:5.9.011。我正在尝试以PDF格式执行HTML布局。我试过的例子提供了site
$html = '<h1>HTML Example</h1>
<h2>List</h2>
Some special characters: < € € € & è è © > \\slash \\\\double-slash \\\\\\triple-slash
';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
//Close and output PDF document
$pdf->Output('example_006.pdf', 'I');
显然发现生成的PDF只有默认页眉和页脚,中间内容为空。
但是,如果我删除特殊字符,如:
$html = '<h1>HTML Example</h1>
<h2>List</h2>
Some special characters:
';
PDF获得$html
中指定的中间内容
在行$ pdf-> writeHTML之前
$html = utf8_decode($html);
也许
$html = utf8_encode($html);
我必须在字符串以UTF8字符结尾后添加单个空格(不是在整个<p>
标记之后):
$name = '<p style="color:red;font-weight:bold;font-size:34pt;text-align:center;">Jaźń Jeżę </p>';
utf_decode()
和utf_encode()
没有用。
正如其他人已经提到的,你需要更改构造函数,设置UTF-8 flase并更改新的charshet,如下所示:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
只有更改此内容并不总能阻止获取空白页面。要防止获取空白页:将字体类型更改为“helvetica”或其他。
$pdf->SetFont('helvetica', '', 11, '', true);
有用!
你需要更改构造函数,设置UTF-8 false并更改新的字符集,如下所示:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
在上面的例子中,我设置编码ISO-8859-1而不是UTF-8,我将true更改为false。
克里斯
我也有类似的问题。我有一个从HTML文件生成pdf,其中包含多个列表和一个刻度符号作为项目符号。
我尝试了所有这些解决方案但没有工作,我不得不使用图片,但它正在减慢过程。最后我碰到了这个:http://www.tcpdf.org/examples/example_055.pdf,最后有一个带有很多符号的“zapfdingbats”字体。
所以我用<span>3<span>
替换了我所有的刻度图像,并将这个元素的字体设置为font-family="zapfdingbats"
,并且它有效。它有点hacky,但它运行良好。
您可以将文本放在变量中并使用php函数进行转换
UTF-8字符的示例
$html = '<h1>HTML Example</h1>
<h2>List</h2>
Some special characters: € è ù à \\ \\\
';
// output the HTML content
$pdf->writeHTML(utf8_encode($html), true, false, true, false, '');
//Close and output PDF document
$pdf->Output('example_006.pdf', 'I');
其他功能使用php reference guide
您正在使用最新版本的TCPDF.Follow步骤提到here。它应该工作
我有类似的问题,我以这种方式解决了所有问题:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', false, true);
最后:
$pdf->writeHTMLCell(0, 0, '', '', utf8_decode($html), 0, 1, 0, true, '', true);
对我来说,这解决了这个问题:
$this->pdf->AddPage('P', 'A4');
$this->pdf->deletePage($this->pdf->getNumPages());
我希望它能帮助别人
我正在将HTML内容写入pdf,并且必须打印复选标记符号。这是我做的,它的工作原理。
$pdf->SetFont('zapfdingbats');
$pdf->SetFont('helvetica'); // for regular content
然后在我的HTML中添加了带有以下代码的复选标记
<span style="font-family: zapfdingbats;">4</span>
希望这个