X
    Categories: HTMLPHP

How to Create PDFs from HTML with PHP and Dompdf

Adobe Systems created PDF to illustrate text and images within a fixed-layout document. The PDF file format is used for downloading data or content from the web application. The PDF file format is ideal for downloading HTML or text content. Converting HTML to PDF is required in order to download web page content as PDF files. This tutorial will demonstrate how to convert HTML to pdf and create PDF files using PHP.

Dompdf is a PHP library that allows you to easily convert HTML to PDF documents. The Dompdf library makes it easy to create PDFs from HTML pages in PHP. This example code will show you how to embed PDF generation functionality into your web application. It also makes it easy to convert HTML to PDF using Dompdf.

To start with, let us install Dompdf. The easiest way to add Dompdf to your project is to use Composer. To see how to install Composer, do follow https://getcomposer.org/download/. Once you have it on your machine, you can navigate to the directory where your web application is located (usually using the cd command) in your Command line/Terminal and type

composer require dompdf/dompdf

This will install Dompdf and all you have to do is include it in your desired files and use it. Composer is a dependency manager similar to npm for Node.js but for PHP and it is a good idea to use it. If you do not wish to use Composer, you can directly download the Dompdf library from https://github.com/dompdf/dompdf/releases and unzip it in your desired folder.

Read Also How to Convert HTML to PDF in PHP with fpdf

Once you have Dompdf, let us create a simple HTML page to show to the user as a PDF. Below is a PHP file that serves as the HTML page of the PDF. It is a dummy page. The PHP script just displays some HTML and fills it with possibly dynamically-generated if you want to add some.

page.php

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

</head>

<body>

     YOUR HTML CONTENT

</body>

</html>

Index.php

<?php

require 'vendor/autoload.php';

// reference the Dompdf namespace

use Dompdf\Dompdf;



// instantiate and use the dompdf class

$dompdf = new Dompdf();

ob_start();



include("page.php")



$html = ob_get_contents();

ob_get_clean();

$dompdf->loadHtml($html);

$dompdf->setPaper('A4', 'portrait');



// Render the HTML as PDF

$dompdf->render();

// Output the generated PDF to Browser

$dompdf->stream();

?>

After we have loaded Dompdf, all we have to do is pass it the HTML page and display the PDF in the user’s browser. We initialize the Dompdf class, then we pass it the HTML page (the page.php) and stream the PDF to the browser.

Now, whenever we access index.php, we will generate pdf using HTML to PDF library Dompdf

If we want to save the PDF to the server for reference – we need only 1-2 more lines of code. We get a string with the contents of the PDF file using $dompdf->output(), save it in a variable or directly put it into a file with an arbitrary name.

Adding one line is sufficient to save the PDF on our server:

file_put_contents("pdfs/file-" . mt_rand(0,99999) . ".pdf",  $dompdf->output());

Although, you would want a strict naming convention and not naming your invoices based on randomness. Unless you are okay with PDFs being overwritten from time to time when chance brings the same numbers.

I guess that’s it. You can now generate cool PDFs without much effort, hopefully.

Conclusion

In this guide we’ve attempted to show an easy way for you to convert HTML into PDF using Dompdf with PHP. Our code example provides the most popular method of configuration to create PDF using PHP. You can easily expand the capabilities of Dompdf by configuring options to meet your requirements. To download all the required documents, such as the Dompdf library Download your source code.

Huzoor Bux: I am a PHP Developer