Your destination for complete Tech news


How to convert HTML to pdf in PHP?

247 0
< 1 min read

There are several ways to convert HTML to PDF using PHP. One option is to use a library such as DOMPDF or mPDF.

To use DOMPDF, you will first need to install it using composer.

composer require dompdf/dompdf

Once DOMPDF is installed, you can use it to convert your HTML to PDF like this:


// Require the DOMPDF autoloader
require_once 'vendor/autoload.php';

// Create a new DOMPDF instance
$dompdf = new Dompdf\Dompdf();

// Load the HTML
$dompdf->loadHtml('<h1>Hello World</h1>');

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

// Render the HTML as PDF

// Output the PDF to the browser

This will create a PDF with the text “Hello World” and display it in the browser. You can also save the PDF to a file by using the save() method instead of stream().

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.