"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How To Convert HTML To PDF With PHP

How To Convert HTML To PDF With PHP

Published on 2024-11-07
Browse:767

(Guide For Windows. Not Mac or Linux)

How To Convert HTML To PDF With PHP

(Image Source)

There's more than one way to convert HTML to a PDF in PHP. You can use Dompdf or Mpdf; however, there is a difference in how these two libraries are doing it.

Note: Not all solutions will be in this article.

To use both these libraries, you will need Composer.

Converting HTML To PDF With Dompdf

I also show the code in the article discussing why the PDF sometimes won't open when using Dompdf.

    Hello World';

        // instantiate and use the dompdf class
        $dompdf = new Dompdf();
        $dompdf->loadHtml($content);

        // (Optional) Setup the paper size and orientation
        $dompdf->setPaper('A4', 'landscape');

        // Render the HTML as PDF
        $dompdf->render();

        ob_end_clean();

        // Output the generated PDF to Browser
        $dompdf->stream();
    ?>

With this code, the PDF is downloaded.

Converting HTML To PDF With Mpdf

WriteHTML('

Hello world! Hi

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

With this code, the PDF is opened in the browser when you navigate to your file (Example: localhost/test1.php)

Release Statement This article is reproduced at: https://dev.to/nifty-little-me/how-to-convert-html-to-pdf-with-php-4eb5?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3