简体   繁体   中英

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

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

I had used the mode on object creation

$mpdf = new Mpdf(['mode' => 'UTF-8']);

Use this if you are sure that your html is utf-8

A combination of this

$mpdf = new Mpdf(['mode' => 'UTF-8']);

and the below worked for me.

$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;

the only thing you have to do to active utf-8 is to add a defult font:

i did this and worket very well so try it and let the others knows if it's a good solotion or not.

just add a defult font and see..

$mpdf = new \Mpdf\Mpdf([
'default_font_size' => 9,
'default_font' => 'Aegean.otf' ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM