Incorrect accentuation when generating PDF

Asked

Viewed 4,536 times

3

After several searches on the net, I could not solve my problem with accentuation (charset).

I am generating a report in FPDF and in the description field comes out all disfigured text:

screenshot

I’ve used the functions utf8_decode and utf8_encode and found that the Mysql database is with the type of charset latin1_swedish_ci.

How do I solve this problem?

3 answers

6


FPDF uses ISO-8859-1 or Windows-1252 encoding, and theoretically, as the collation Mysql is latin1_swedish_ci as strings do not need to be converted.

If it doesn’t work without the conversion it is possible to convert to Windows-1252 using the code:

iconv(mb_detect_encoding($str), 'windows-1252', $str);

mb_detect_encoding() is used to return the charset correct for iconv(), who makes the conversion.

Source: FPDF utf-8 encoding (HOW-TO)

  • Thank you Sanction, it worked.

2

  • The problem with this is that if the text is not in utf8, the Samson solution, if you haven’t tested it yet, would be interesting to analyze, it first checks the current encoding and then converts, so there are other types of encodings that may appear and utf8_decode won’t solve.

  • Use this never had problems.... solved everything.... I modified my FPDF 1.81 with TFPDF... something else.... most of the text editors are UTF8...as Notepad++

  • I understand friend, but I refer to what is not utf8, because there are many encodings beyond, and combined mb_detect_encoding+iconv becomes smarter.

  • I just think that if you have 50 lines... repeat this method is not very nice.... even utf8_decode the ideal and change the FPDF to your need. And still has TCPDF which is an alternative...

1

It is always important to check the charset both in class domPDF, tcpdf, fPDF... how much in the file php which is generated, in the html and also in the data recovered from the Database. Don’t forget to set at the beginning of your php file:

header("Content-Type: text/html; charset=UTF-8");

If your PDF generating class allows it, try using utf8_encode, for example, if your HTML is stored in a variable $html, apply the Encode as below:

$html = utf8_encode($html);
$pdf->WriteHTMLCell(192,0,9,'',$html,0,1);//Nesse caso estou utilizando o tcpdf

Browser other questions tagged

You are not signed in. Login or sign up in order to post.