how to convert dynamic php file to pdf?

纵然是瞬间 提交于 2019-12-03 21:55:48

First you'll need to generate HTML from your PHP, then pass it to DOMPDF:

<?php
    require_once("dompdf_config.inc.php");
    ob_start();
    require_once("path/to/input/file.php");
    $dompdf = new DOMPDF();
    $dompdf->load_html(ob_get_clean());
    $dompdf->render();
    $dompdf->stream("file.pdf");
?>

You can also do a regular HTTP request:

<?php
    require_once("dompdf_config.inc.php");
    $dompdf = new DOMPDF();
    $dompdf->load_html_file('http://example.com/file.php');        
    $dompdf->render();
    $dompdf->stream("file.pdf");
?>    

If you need JavaScript support, try wkhtmltopdf, it's based on Webkit and does it's work perfectly.

You can use ob_start and ob_get_contents to run PHP code and capture the output as a string.

For the JavaScript chart, though, you're out of luck. DOMPDF is pretty smart, but it's not that smart. You'll need to either use a non-JavaScript chart solution, do without the charts, or use a web browser to generate the PDF.

I am not sure why you need to generate HTML to build a PDF in the first place but as others have suggested, build out your PHP script and then use something like FPDF or TCPDF.

They both build PDFs just fine and can take HTML input.

Try This ...

<?php
    ob_start();

    require_once("dompdf_config.inc.php");

    $file = file_get_contents('http://example.com/file.php');

    $dompdf = new DOMPDF();
    $dompdf->load_html($file);
    $dompdf->render();
    $dompdf->stream("filename.pdf");
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!