TCPDF HTML with Special Characters displays empty

2019-01-18 12:47发布

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

$html = '<h1>HTML Example</h1>
<h2>List</h2>
Some special characters: &lt; € &euro; &#8364; &amp; è &egrave; &copy; &gt; \\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.

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

8条回答
倾城 Initia
2楼-- · 2019-01-18 12:58

You are using latest version of TCPDF.Follow steps mentioned here. It should work

查看更多
可以哭但决不认输i
3楼-- · 2019-01-18 13:02

you need to change constructor, Set UTF-8 flase and change new charshet like below:

$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.

Krish

查看更多
Root(大扎)
4楼-- · 2019-01-18 13:07

You can put the text in a variable and use a php function to convert

example for UTF-8 caracter

    $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

查看更多
【Aperson】
5楼-- · 2019-01-18 13:10

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.

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.

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.

查看更多
Melony?
6楼-- · 2019-01-18 13:12

As already mentionned by other persons you need to change the constructor, Set UTF-8 flase and change new charshet like below:

$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.

$pdf->SetFont('helvetica', '', 11, '', true);

It works!

查看更多
等我变得足够好
7楼-- · 2019-01-18 13:18

For me, this fixed the problem :

$this->pdf->AddPage('P', 'A4');
$this->pdf->deletePage($this->pdf->getNumPages());

I hope it will help someone

查看更多
登录 后发表回答