TCPDF HTML with Special Characters displays empty PDF file
Asked Answered
K

12

16

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

Knur answered 10/11, 2010 at 6:40 Comment(0)
O
15

before line $pdf->writeHTML

$html = utf8_decode($html);

maybe

$html = utf8_encode($html);
Ottinger answered 8/8, 2014 at 15:48 Comment(1)
utf8_decode did the trick for me! thanks, was getting really frustrating with no error message to guide meBreeden
S
9

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!

Sequestration answered 9/2, 2013 at 15:18 Comment(0)
P
5

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

Petulah answered 26/5, 2011 at 7:8 Comment(1)
set the $unicode parameter on TCPDF constructor to false and the $encoding parameter to 'ISO-8859-1' or other character mapPetulah
M
3

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.

Marjana answered 22/1, 2016 at 10:29 Comment(1)
How did you calculate 3 in span. please explain this.Judejudea
C
3

I am writing HTML content to pdf and have to print the check mark symbol. Here is what I did and it worked.

$pdf->SetFont('zapfdingbats');
$pdf->SetFont('helvetica'); // for regular content

and then in my HTML I added the checkmark with below code

<span style="font-family: zapfdingbats;">4</span>

Hope this

Commencement answered 21/1, 2019 at 17:41 Comment(0)
P
1

For me, this fixed the problem :

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

I hope it will help someone

Promulgate answered 6/4, 2017 at 12:53 Comment(1)
This worked for me with dejavusans font and for the french language. Thanks. You saved my life.+1Allowable
I
1

For me worked saving file with UTF-8 encoding. No changing new TCPDF() or adding utf8_decode($html) needed. Now text with special characters generates normally.

Impatience answered 8/1, 2021 at 9:44 Comment(0)
T
0

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

Thief answered 10/11, 2010 at 7:27 Comment(1)
I am following same steps only. But no luck.Knur
F
0

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);
Fulgurate answered 23/5, 2015 at 11:14 Comment(0)
P
0

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

Pattani answered 30/3, 2016 at 14:7 Comment(0)
I
0

I had to add single space after string ending with UTF8 character (not after whole <p> tag):

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

Inesinescapable answered 12/3, 2019 at 15:7 Comment(0)
A
0

Use SetFont with dejavusans to use special characters to print in pdf with html like ő Ő ű Ű

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

Aerosphere answered 4/4, 2020 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.