How to Create PDF in PHP using Dompdf `

In this tutorial, we are going to show you how to show the user dynamically-generated PDFs from your web application that relies on PHP. We are going to use a third-party library called Dompdf that enables us to create PDFs and save them to the server or directly display them.

Setup dompdf library

PHP Code

<?php
	$pdfname = 'billinvoice.pdf';
	require_once 'dompdf/autoload.inc.php';

	use Dompdf\Dompdf;
	$dompdf = new Dompdf();

	$dompdf->loadHtml('Your aticle');

	$dompdf->setPaper('A4', 'landscape');
	$dompdf->render();
	$output = $dompdf->output();
	file_put_contents('assets/'.$pdfname.'', $output);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *