5

I am using tFPDF to generate a PDF. The php file is UTF-8 encoded. I want © for example, to be output in the pdf as the copyright symbol.

I have tried iconv, html_entity_decode, htmlspecialchars_decode. When I take the string I am trying to decode and hard-code it in to a different file and decode it, it works as expected. So for some reason it is not being output in the PDF. I have tried output buffering. I am using DejaVuSansCondensed.ttf (true type fonts).

Link to tFPDF: http://fpdf.org/en/script/script92.php

I am out of ideas. I tried double decoding, I checked everywhere to make sure it was not being encoded anywhere else.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Base Desire
  • 485
  • 1
  • 5
  • 15
  • [Possible dup](http://stackoverflow.com/questions/9327960/convert-delascii-characters-delinsentity-codes-ins-to-utf8-in-mysql-on-t), with no answer `:(` – halfer May 09 '12 at 21:50

5 Answers5

13

you need this:

iconv('UTF-8', 'windows-1252', html_entity_decode($str));

the html_entity_decode decodes the html entities. but due to any reason you must convert it to utf8 with iconv. i suppose this is a fpdf-secret... cause in normal browser view it is displayed correctly.

emfi
  • 649
  • 6
  • 23
  • in any pdf-output you wanna make. for example: $pdf->Write(5, iconv('UTF-8', 'windows-1252', html_entity_decode($str))); – emfi Nov 25 '14 at 15:07
3

Actully, fpdf project FAQ has an explanation for it:

http://www.fpdf.org/~~V/en/FAQ.php#q7

Don't use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252. It is possible to perform a conversion to ISO-8859-1 with utf8_decode():

$str = utf8_decode($str); 

But some characters such as Euro won't be translated correctly. If the iconv extension is available, the right way to do it is the following:

$str = iconv('UTF-8', 'windows-1252', $str);

So, as emfi suggests, a combination of iconv() and html_entity_decode() PHP functions is the solution to your question:

$str = iconv('UTF-8', 'windows-1252', html_entity_decode("©"));
abu
  • 422
  • 7
  • 14
0

I'm pretty sure there is no automatic conversion available from HTML entity codes to their UTF-8 equivalents. In cases like this I have resorted to manual string replacement, eg:

$strOut = str_replace( "©", "\xc2\xa9", $strIn );
JamesG
  • 4,288
  • 2
  • 31
  • 36
0

I have fix the problem with this code:

$str = utf8_decode($str);
$str = html_entity_decode($str);
$str =  iconv('UTF-8', 'windows-1252',$str);
0

You can also use setFont('Symbol') or setFont('ZapfDingbats') to select the special characters that you want to print.

define('TICK', chr(214)); # in font 'Symbol' -> print a tick symbol
...
$this->SetFont('Symbol', 'B', 8);
$this->Cell(5, 5, TICK, 0, 'L');    # will output the symbol to PDF

Output: √

This way, you won't need to convert to ISO-8859-1 or Windows-1252 OR use another library tFPDF for special characters :)

Refer: http://www.fpdf.org/en/script/script4.php for font & character list

tbraun89
  • 2,246
  • 3
  • 25
  • 44
tam
  • 1
  • 1