PASSWORD RESET

Your destination for complete Tech news

PHP

How to convert HTML to pdf in PHP?

649 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:

<?php

// 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
$dompdf->render();

// Output the PDF to the browser
$dompdf->stream();

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.