Special Characters in FPDF with PHP

Viewed 88335

I have a web form that users can fill out and that content fills up a PDF with FPDF and PHP. When a user enters a word with an apostrophe, a slash appears before it on the PDF.

Similarly, special characters like trademark symbols are encoded wrong.

The FPDF FAQs say to use:

$str = utf8_decode($str);

But I'm just not sure how to apply that to the whole PDF. I'm trying to think about it as if it was an HTML page but that isn't helping.

Any ideas?

11 Answers

none of above solutions worked for me, so I solved the problem like this:

$this->AddFont('Arial','','arial.php');
$this->SetFont('Arial','',12);
$this->Cell(0,5,iconv("UTF-8", "CP1250//TRANSLIT", $string),0,1,'L');

Before trying the above lines, do the following:

Copy from c:/Windows/Fonts/Arial.ttf to the /tutorial folder of FPDF.

Edit the content of makefont.php

require('../makefont/makefont.php');
MakeFont('arial.ttf','cp1250');

Execute makefont.php

Copy the following files to the /font folder of FPDF: arial.php arial.ttf arial.z

Finally, define the "font folder". Open fpdf.php (main library file) and add:

define('FPDF_FONTPATH','font');

The PDF works for me with all special characters, I believe it was the problem in the Arial font itself, which FPDF originally uses. It should work with other fonts aswell, if they support your characters. Good luck!

Below works for me (Using FPDF):


function em_jaz($word) {
$word = str_replace('+', ' ', $word);
$word = str_replace("%C3%A9","%E9",$word);          /* é */
$word = str_replace("%C3%A8","%E8",$word);          /* è */
$word = str_replace("%C3%AE","%EE",$word);          /* î */
$word = str_replace("%26rsquo%3B","%27",$word);     /* ' */
$word = str_replace("%C3%89","%C9",$word);          /* É */
$word = str_replace("%C3%8A","%CA",$word);          /* Ê */ 
$word = str_replace("%C3%8B","%CB",$word);          /* Ë */
$word = str_replace("%C3%8C","%CC",$word);          /* Ì */
$word = str_replace("%C3%8D","%CD",$word);          /* Í */
$word = str_replace("%C3%8E","%CE",$word);          /* Î */
$word = str_replace("%C3%8F","%CF",$word);          /* Ï */
$word = str_replace("%C3%90","%D0",$word);          /* Ð */
$word = str_replace("%C3%91","%D1",$word);          /* Ñ */
$word = str_replace("%C3%92","%D2",$word);          /* Ò */
$word = str_replace("%C3%93","%D3",$word);          /* Ó */
$word = str_replace("%C3%94","%D4",$word);          /* Ô */
$word = str_replace("%C3%95","%D5",$word);          /* Õ */
$word = str_replace("%C3%96","%D6",$word);          /* Ö */
$word = str_replace("%C3%98","%D8",$word);          /* Ø */                 
$word = str_replace("%C3%99","%D9",$word);          /* Ù */
$word = str_replace("%C3%9A","%DA",$word);          /* Ú */
$word = str_replace("%C3%9B","%DB",$word);          /* Û */
$word = str_replace("%C3%9C","%DC",$word);          /* Ü */
$word = str_replace("%C3%9D","%DD",$word);          /* Ý */
$word = str_replace("%C3%9E","%DE",$word);          /* Þ */
$word = str_replace("%C3%9F","%DF",$word);          /* ß */
$word = str_replace("%C3%A0","%E0",$word);          /* à */
$word = str_replace("%C3%A1","%E1",$word);          /* á */
$word = str_replace("%C3%A2","%E2",$word);          /* â */
$word = str_replace("%C3%A3","%E3",$word);          /* ã */
$word = str_replace("%C3%A4","%E4",$word);          /* ä */
$word = str_replace("%C3%A5","%E5",$word);          /* å */
$word = str_replace("%C3%A6","%E6",$word);          /* æ */
$word = str_replace("%C3%A7","%E7",$word);          /* ç */
$word = str_replace("%C3%AA","%EA",$word);          /* ê */
$word = str_replace("%C3%AB","%EB",$word);          /* ë */
$word = str_replace("%C3%AC","%EC",$word);          /* ì */
$word = str_replace("%C3%AD","%ED",$word);          /* í */
$word = str_replace("%C3%AF","%EF",$word);          /* ï */
$word = str_replace("%C3%B0","%F0",$word);          /* ð */
$word = str_replace("%C3%B1","%F1",$word);          /* ñ */
$word = str_replace("%C3%B2","%F2",$word);          /* ò */
$word = str_replace("%C3%B3","%F3",$word);          /* ó */
$word = str_replace("%C3%B4","%F4",$word);          /* ô */
$word = str_replace("%C3%B5","%F5",$word);          /* õ */
$word = str_replace("%C3%B6","%F6",$word);          /* ö */
$word = str_replace("%C3%B7","%F7",$word);          /* ÷ */
$word = str_replace("%C3%B8","%F8",$word);          /* ø */
$word = str_replace("%C3%B9","%F9",$word);          /* ù */
$word = str_replace("%C3%BA","%FA",$word);          /* ú */
$word = str_replace("%C3%BB","%FB",$word);          /* û */
$word = str_replace("%C3%BC","%FC",$word);          /* ü */
$word = str_replace("%C3%BD","%FD",$word);          /* ý */
$word = str_replace("%C3%BE","%FE",$word);          /* þ */
$word = str_replace("%C3%BF","%FF",$word);          /* ÿ */ 
$word = str_replace("%40","%40",$word);             /* @ */
$word = str_replace("%60","%60",$word);             /* ` */
$word = str_replace("%C2%A2","%A2",$word);          /* ¢ */
$word = str_replace("%C2%A3","%A3",$word);          /* £ */
$word = str_replace("%C2%A5","%A5",$word);          /* ¥ */
$word = str_replace("%7C","%A6",$word);             /* | */
$word = str_replace("%C2%AB","%AB",$word);          /* « */
$word = str_replace("%C2%AC","%AC",$word);          /* ¬ */
$word = str_replace("%C2%AF","%AD",$word);          /* ¯ */
$word = str_replace("%C2%BA","%B0",$word);          /* º */
$word = str_replace("%C2%B1","%B1",$word);          /* ± */
$word = str_replace("%C2%AA","%B2",$word);          /* ª */
$word = str_replace("%C2%B5","%B5",$word);          /* µ */
$word = str_replace("%C2%BB","%BB",$word);          /* » */
$word = str_replace("%C2%BC","%BC",$word);          /* ¼ */
$word = str_replace("%C2%BD","%BD",$word);          /* ½ */
$word = str_replace("%C2%BF","%BF",$word);          /* ¿ */
$word = str_replace("%C3%80","%C0",$word);          /* À */
$word = str_replace("%C3%81","%C1",$word);          /* Á */
$word = str_replace("%C3%82","%C2",$word);          /* Â */
$word = str_replace("%C3%83","%C3",$word);          /* Ã */
$word = str_replace("%C3%84","%C4",$word);          /* Ä */
$word = str_replace("%C3%85","%C5",$word);          /* Å */
$word = str_replace("%C3%86","%C6",$word);          /* Æ */
$word = str_replace("%C3%87","%C7",$word);          /* Ç */
$word = str_replace("%C3%88","%C8",$word);          `/`* È */
return $word;
}

$content = urlencode($content);

$content = urldecode($pdf->em_jaz($content));

I have used this as $str = preg_replace('/[^A-Za-z0-9_-]/','', $str); for me has been enough, I eliminate the not alphanumerical characters

This did it for me: iconv("UTF-8", "ISO-8859-1//TRANSLIT//IGNORE", your-text-var-here)

Why not use the function PDF recommends?

$text = utf8_decode($text);

It works for me.

you have to encode it this way assuming your special character is "°C"

> $pdf->Cell(X(int),Y(int),iconv("UTF-8", "CP1250//TRANSLIT", '°C'));
Related