"HTML contains invalid UTF-8 Character(s)" error when using mpdf

Asked

Viewed 7,725 times

8

I have a problem using mPDF. Basically, I have a code that makes a select of a table and then displays the values in a PDF page.

The problem is that if any table value has an accent (example: "Hello") the error appears:

"HTML contains invalid UTF-8 Character(s)"

If I remove the accent, the value is displayed correctly. Is there any way to keep the accents?

Here’s my code if anyone wants to take a look:

<?php

ob_start();  

?>

<html>

<head>
</head>

<body>

<?php       

mysql_connect("localhost","root");
mysql_select_db("db1") or die(mysql_error());

$count=mysql_query("SELECT * FROM teste_2");
$count_fin=mysql_num_rows($count);
$x=1;

while($x<=$count_fin){
    //$id = $_POST['id'];
    $sql=mysql_query("SELECT questao from db1.teste_2 where id_quest=$x");
    while($info = mysql_fetch_array($sql)){
        $Array[$x]=$info['questao'];
    }

    echo $Array[$x];  
    echo '<hr>';     
    echo'<br>';     
    $x = $x + 1;    
}

$html='<php echo $count_fin;?>'; //setcookie ("Nome_ques", "", time() - 3600); ?>
    </body>

    </html>
    <?php

$HTMLoutput= ob_get_contents();
ob_end_clean();

//
include("mpdf/MPDF57/mpdf.php");  
$mpdf=new mPDF();  
$mpdf->WriteHTML($HTMLoutput);  
$mpdf->Output();   

?>

I thank anyone who can help.

  • 2

    I already found the solution. Just add $mpdf->charset_in='windows-1252';

  • 1

    Put your solution with an answer stating that the MPDF needs the indication of charset in order to correctly work the values. Then mark your answer as accepted to close this topic.

3 answers

3


I already found the solution. Just add $mpdf->charset_in='windows-1252'.

Example:

include("mpdf/MPDF57/mpdf.php");  
$mpdf=new mPDF();  
$mpdf->WriteHTML($HTMLoutput);  
$mpdf->Output();   
$mpdf->charset_in='windows-1252';

2

I had the same problem I could solve just like this:

$html = utf8_encode($html1);

source: https://stackoverflow.com/a/28736069

In my case the pdf is formatted with utf8

$mpdf = new mPDF('utf-8', 'A4-R');

Throughout the pdf there are accents and special characters that do not generate error, however when user the strftime function to format date spelled out is that the error occurred when the weekday is Tuesday.

I used the solution only in this function as follows:

utf8_encode((strftime('%A, %d de %B de %Y', strtotime($data))))

-1

So that there is no break in the charset and do not need to keep changing each special characters because the above solution brings another transfer of being able to happen the errors in other characters that do not come from the table

For example, just put:

utf8_encode($tabela['title'];)

Browser other questions tagged

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