Special Characters in FPDF with PHP

Carson picture Carson · Aug 18, 2010 · Viewed 73.8k times · Source

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?

Answer

Carson picture Carson · Aug 25, 2010

Figured this out by doing the following (pagesubtitle is the name of the text field in the form):

$reportSubtitle = stripslashes($_POST['pagesubtitle']);
$reportSubtitle = iconv('UTF-8', 'windows-1252', $reportSubtitle);

Then print it out:

$pdf->Write (6, $reportSubtitle);

This will remove any unwanted slashes following apostrophes, as well as use the 'iconv' function to print special characters such as ™