Error "HTML contains invalid UTF-8 character (s)" when using mpdf

8

I have a problem using mPDF. Basically, I have a code that makes a select of a table and then displays the values on 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();   

?>

Thanks to anyone who can help.

    
asked by anonymous 06.07.2014 / 15:57

3 answers

3

I have 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';
    
07.07.2014 / 19:25
1

I had the same problem that I could solve only this way:

$html = utf8_encode($html1);

source: link

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 an error, but when user the strftime function to format date in full is that the error occurred when the day of week is Tuesday.

I used the solution only in this function as follows:

utf8_encode((strftime('%A, %d de %B de %Y', strtotime($data))))
    
31.08.2016 / 19:47
0

So that there is no break in the charset and you do not have to keep changing each special character because the above solution brings another thread so that errors can happen in other characters that do not come from the table

For example, just put:

utf8_encode($tabela['title'];)
    
11.10.2017 / 13:09