如何在PHP中将HTML页面转换为PDF

时间:2016-01-13 12:49:06

标签: php html pdf mpdf

<?php
    include ('mpdf/mpdf.php');
    $mpdf = new mpdf;
    ob_start();
?>

<html>
    <head></head>
    <body>
       My various content with table, dropdown menu and 2 include files.
    </body>
</html>

<?php 
    $html = ob_get_contents();
    ob_end_clean();

    $mpdf->Output();
    exit;
?>

想知道为什么这个功能不起作用,只会输出空的pdf文件。我尝试了很多方法,但它没有用。我已将此函数放在代码的开头,但它总是输出一个空文件。

1 个答案:

答案 0 :(得分:1)

试试这段代码: -

 <?php 
        $html = ob_get_contents();
        ob_end_clean();
        // You need to write html
        $mpdf->WriteHTML($html);
        // define path of your output file and set mode 'F' for saving
        $mpdf->Output('filename.pdf','F');
        exit;
 ?>