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 P
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 ™
I have used this as $str = preg_replace('/[^A-Za-z0-9_-]/','', $str);
for me has been enough, I eliminate the not alphanumerical characters
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!
None of above had solved my problem. I had solved it by the following way:
setlocale(LC_CTYPE, 'en_US');
$value = iconv('UTF-8', 'ASCII//TRANSLIT', $value);
$fpdf->Cell(140, 6, $value, 1);
Hope you will be helpful.
Reference: Link
Try this simple function: utf8_encode($txt)
. It works for me.
This class is a modified version of FPDF that adds UTF-8 support. Moreover, it embeds only the necessary parts of the fonts that are used in the document, making the file size much smaller than if the whole fonts were embedded. These features were originally developed for the mPDF project.
http://fpdf.org/en/script/script92.php