mPDF& char不编码,我怎么办?

时间:2015-10-26 15:20:44

标签: php html mpdf

mPDF不转换字符'&并且使得以下所有内容都无法翻译。生成pdf但是字符'&amp ;;之后的所有代码'没有打印。这是我的代码:

<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');
?> 

3 个答案:

答案 0 :(得分:0)

使用htmlspecialchars

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

答案 1 :(得分:0)

试试这个:

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

您应该将其添加到HTML的主题部分:

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

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

答案 2 :(得分:0)

After days I found the problem. The ajax call not coded character and now adding "encodeURIComponent (divcontent)" and passing the result to the php function, should not use "html_entity_decode", mPDF print special characters such as "&".