使用html2pdf库创建HTML到PDF文件

时间:2017-08-04 11:52:36

标签: jquery wordpress html-to-pdf

我正在使用html2pdf库在wordpress中生成pdf文件。使用此我可以创建pdf文件,但是当我尝试打开此文件时,它显示错误:"格式错误:不是pdf或已损坏"。 我的代码是:

 $content = "<page><h1>Exemple d'utilisation</h1><br>Ceci est un <b>exemple d'utilisation</b>de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br></page>";
    include('html2pdf/html2pdf.class.php');
    try
    {
        ob_end_flush();
        ob_get_clean();
        ob_end_clean();

        $html2pdf = new HTML2PDF('L', 'Legal', 'fr');
        $html2pdf->pdf->SetDisplayMode('fullpage');
        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content);
        $html2pdf->Output("report.pdf",'D');
    }
    catch(HTML2PDF_exception $e) {
        echo 'exceptional error='.$e;
        exit;
    }

请说明我的错误。

提前致谢。

1 个答案:

答案 0 :(得分:2)

Maybe, pdf isn't closed correctly, try to add exit; at the end of try-block:

try
{
   ...
   exit;
}

And you are sure that Output must be start with capital letter?