mPDF & char 未编码,我该怎么做?

mPDF & char is not encoded, how i can do?

mPDF 不转换字符 '&' 并使后面的所有内容不可翻译。 pdf 已生成,但未打印字符“&”后的所有代码。这是我的代码:

<table>
    <tr>
        <p>test example & test example</p>
    </tr>
</table>

我使用此 php 代码创建 pdf 输出:

<?php
$divcontent = $_POST['divcontent'];
$html='<html><head></head><body style="background-color:#FFFFFF;height:100%; width:100%;">';
$html.= $divcontent;
$html.='</body></html>';
//==============================================================

include(dirname(__FILE__)."/../../libs/MPDF57/mpdf.php");
@$mpdf=new mPDF('c');
@$mpdf->SetDisplayMode('fullpage');
@$stylesheet = file_get_contents(realpath(dirname(__FILE__)."/../..")."/css/style.css");
$mpdf->setFooter('{PAGENO}');
@$mpdf->WriteHTML($stylesheet,1);
@$mpdf->WriteHTML($html);

$rand = rand();
@$mpdf->Output(realpath(dirname(__FILE__)."/../..")."/file.pdf",'F');
?> 

使用htmlspecialchars

<table>
    <tr>
        <p>test example <?php echo htmlspecialchars('&') ?> test example</p>
    </tr>
</table>

试试这个:

$mpdf->charset_in='utf-8';

您应该将其添加到 HTML 的开头部分:

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

确保您的服务器 Apache 或 Nginx 也使用 utf-8 作为默认字符集。

几天后我发现了问题。 ajax 调用未编码字符,现在添加 "encodeURIComponent (divcontent)" 并将结果传递给 php 函数,不应使用 "html_entity_decode",mPDF 打印特殊字符,例如“&”。