php 带有特殊字符的 TCPDF HTML 显示空的 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4141865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TCPDF HTML with Special Characters displays empty PDF file
提问by Asif Mulla
I am using TCPDF Library Version: 5.9.011. I am trying to execute HTML layout as PDF. For which I tried with example provide with the site
我正在使用 TCPDF 库版本:5.9.011。我正在尝试将 HTML 布局作为 PDF 执行。为此,我尝试在网站上提供示例
$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');
Apparently found that generated PDF only default header and footer with middle content blank.
显然发现生成的PDF只有默认页眉和页脚,中间内容为空白。
However if I remove special characters like:
但是,如果我删除特殊字符,如:
$html = '<h1>HTML Example</h1>
<h2>List</h2>
Some special characters:
';
PDF gets its middle content as specified in $html
PDF 获取其中指定的中间内容 $html
采纳答案by anonimus
before line $pdf->writeHTML
在行$pdf->writeHTML 之前
$html = utf8_decode($html);
maybe
也许
$html = utf8_encode($html);
回答by Fré
As already mentionned by other persons you need to change the constructor, Set UTF-8 flase and change new charshet like below:
正如其他人已经提到的,您需要更改构造函数,设置 UTF-8 flase 并更改新的字符表,如下所示:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
Only changing this won't always prevent getting a blank page. To prevent getting a blank page: change your font type to 'helvetica' or other.
仅更改此项并不总是能防止出现空白页。为防止出现空白页:将字体类型更改为“helvetica”或其他。
$pdf->SetFont('helvetica', '', 11, '', true);
It works!
有用!
回答by Elisa
you need to change constructor, Set UTF-8 flase and change new charshet like below:
您需要更改构造函数,设置 UTF-8 flase 并更改新的字符表,如下所示:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
In above example, I set encoding ISO-8859-1 instead of UTF-8 and I change true to false.
在上面的示例中,我将编码设置为 ISO-8859-1 而不是 UTF-8,并将 true 更改为 false。
Krish
克里什
回答by floflo88
I also had a similar issue. I had a to generate a pdf from a HTML file with multiple lists and a tick symbol as a bullet point.
我也有类似的问题。我必须从一个 HTML 文件生成一个 pdf 文件,其中包含多个列表和一个刻度符号作为项目符号。
I tried all these solutions but none worked, I had to used a picture, but it was slowing down the process. Finally I came accross this: http://www.tcpdf.org/examples/example_055.pdf, at the very end there is a "zapfdingbats" font with a lot of symbols.
我尝试了所有这些解决方案,但都没有奏效,我不得不使用图片,但它拖慢了进程。最后我遇到了这个:http: //www.tcpdf.org/examples/example_055.pdf,最后有一个带有很多符号的“zapfdingbats”字体。
So I replaced all my tick images by : <span>3<span>
and set the font of this element to font-family="zapfdingbats"
, and it worked.It is a bit hacky, but it works well.
所以我用 : 替换了我所有的刻度图像,<span>3<span>
并将这个元素的字体设置为font-family="zapfdingbats"
,它起作用了。它有点hacky,但效果很好。
回答by Nagarjun
I am writing HTML content to pdf and have to print the check mark symbol. Here is what I did and it worked.
我正在将 HTML 内容写入 pdf,并且必须打印复选标记符号。这就是我所做的,并且奏效了。
$pdf->SetFont('zapfdingbats');
$pdf->SetFont('helvetica'); // for regular content
and then in my HTML I added the checkmark with below code
然后在我的 HTML 中,我添加了带有以下代码的复选标记
<span style="font-family: zapfdingbats;">4</span>
Hope this
希望这个
回答by long
I had to add single space after string ending with UTF8 character (not after whole <p>
tag):
我不得不在以 UTF8 字符结尾的字符串后添加一个空格(而不是在整个<p>
标签之后):
$name = '<p style="color:red;font-weight:bold;font-size:34pt;text-align:center;">Ja?ń Je?? </p>';
utf_decode()
and utf_encode()
didn't work.
utf_decode()
并utf_encode()
没有工作。
回答by vishu
Use SetFont with dejavusans to use special characters to print in pdf with html like ? ? ? ?
将 SetFont 与 dejavusans 一起使用以使用特殊字符以 html 格式打印 pdf,例如 ? ? ? ?
Also make sure in html you don't need to use font-family other wish that font family will overwrite the dejavusans, and you will not get what you looking for.
还要确保在 html 中您不需要使用 font-family 其他希望字体系列会覆盖 dejavusans,并且您不会得到您想要的东西。
$html = '<h1>HTML Example</h1>
<h2>List</h2>
Some special characters: < € € & è è © > \slash
\\double-slash \\\triple-slash
more char ? ? ? ?
';
$pdf->SetFont('dejavusans');
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('output.pdf', 'D');
Hope this will help
希望这会有所帮助
回答by Ossarotte
You can put the text in a variable and use a php function to convert
您可以将文本放入变量中并使用 php 函数进行转换
example for UTF-8 caracter
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');
for other function use php reference guide
对于其他功能,请使用php 参考指南
回答by Julien Gustin
For me, this fixed the problem :
对我来说,这解决了问题:
$this->pdf->AddPage('P', 'A4');
$this->pdf->deletePage($this->pdf->getNumPages());
I hope it will help someone
我希望它会帮助某人
回答by John
I had a similar problem and I fixed all in this way:
我有一个类似的问题,我以这种方式解决了所有问题:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', false, true);
and at the end:
最后:
$pdf->writeHTMLCell(0, 0, '', '', utf8_decode($html), 0, 1, 0, true, '', true);