Search code examples
phpjquerypdf-generationmpdf

Library Mpdf (php): Set utf-8 and use WriteHTML with utf-8


I need help with the php Mpdf library. I am generating content for a pdf, it is in a div tag, and sent by jquery to the php server, where Mpdf is used to generate the final file.

In the generated pdf file the utf-8 characters go wrong, for example "generación" instead of "generación".

I detail how they are implemented:

HTML

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Sending content for pdf (jquery)

$('#pdf').click(function() {       
    $.post( 
            base_url,
            { 
                contenido_pdf:      $("#div").html(),
            },
            function(datos) {
            }       
    );
});

Reception content (PHP)

$this->pdf = new mPDF();
$this->pdf->allow_charset_conversion = true;                                
$this->pdf->charset_in = 'iso-8859-1';
$contenido_pdf  = this->input->post('contenido_pdf');

$contenido_pdf_formateado = mb_convert_encoding($contenido_pdf, 'UTF-8', 'windows-1252');
$this->m_pdf->pdf->WriteHTML($contenido_pdf_formateado);

Other tested options:

1. $this->pdf->charset_in = 'UTF-8';

Get error:

Severity: Notice  --> iconv(): Detected an illegal character in input string

2.

$contenido_pdf_formateado = mb_convert_encoding($contenido_pdf, 'UTF-8', 'UTF-8');

or

3.

$contenido_pdf_formateado = utf8_encode($contenido_pdf);

Get incorrect characters, like the original case.

What is wrong or what is missing to see the text well? Thanks


Solution

  • Solution

    $contenido_pdf_formateado = utf8_decode($contenido_pdf);
    $this->m_pdf->pdf->WriteHTML($contenido_pdf_formateado);