Special characters with dompdf and php

2019-06-09 01:52发布

问题:

I have a question: I tried to export a pdf with dompdf and php but I can't do this and I dont understand where is my problem, so my code is:

public function generateTitlePage($company)
{
    $this->load->library('dompdf_gen');
    $html='
        <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
            <div style="margin-top:20px;text-align: center;font-weight: bold">
                Company:'.$company.'
            </div>
        </body>
        <html>'; 
    $dompdf = new DOMPDF();
    $html = stripslashes($html);
    $dompdf->load_html($html, 'UTF-8');
    $dompdf->set_paper('a4', 'portrait');
    $dompdf->render();
    $dompdf->stream("welcome.pdf");
}

For example if I have ă in my pdf this symbol is converted in : %C4%83

回答1:

I tried to find similar and I found, that many other users has the same problem with UTF-8 encoding. They found solution by changing mbstring.encoding_translation to On in php.ini configuration file:

mbstring.encoding_translation = On

Or for some helped utf8_decode() function:

$dompdf->load_html(utf8_decode($html), 'UTF-8');