Convert HTML to PDF using PHP

To illustrate text and graphics in a fixed-layout document, Adobe Systems invented PDF. When downloading information or material from a web application, PDF files are used. Downloading HTML or text content is made a lot easier with the PDF file format. To download the content of web pages as PDF files, HTML must be converted to PDF. This tutorial will show you how to use PHP to create PDF files and convert HTML to PDF.

Dompdf is a PHP package that makes it simple to convert HTML files to PDF files. The Dompdf package makes it simple to generate PDF files in PHP from HTML sites. You can incorporate PDF creation capability into your web application by using the example code provided. Additionally, it makes HTML conversion simple.

INSTRUCTION

Let's install Dompdf to get things started. Using Composer is the simplest approach to incorporate Dompdf into your project. Please go to https://getcomposer.org/download/ to learn how to install Composer. Once it is installed on your computer, you may use the Command line/Terminal to go to the directory where your web application is located (often using the cd command) and type

composer require dompdf/dompdf

All you need to do to use Dompdf after this installation is to include it in the files you want to utilize it in. It is a good idea to utilize Composer, a dependency manager for PHP that is analogous to npm for Node.js. If you don't want to use Composer, you can download and unzip the Dompdf library straight from https://github.com/dompdf/dompdf/releases.

Once you have Dompdf, let's make a straightforward HTML page that the viewer may see as a PDF. Here is a PHP file that acts as the PDF's HTML page. This page is a dummy. When you want to add some, the PHP script simply shows some HTML and fills it with possible dynamic content.

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 DompdfDompdf; // 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();
?>

Dompdf only needs to be loaded once, and then we can send it an HTML page to display the PDF in the user's browser. The Dompdf class is initialized before being given the HTML page (the page.php) and the PDF to stream to the browser.

Now, everytime we access index.php, Dompdf's HTML to PDF library will create a pdf.

Only a few additional lines of code are required to store the PDF to the server for future use. Using $dompdf->output(), we may obtain a string containing the contents of the PDF file and store it in a variable or directly write it to a file with any name we choose.

To save the PDF to our server, just add one line:

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

However, you should adhere to a precise naming convention and refrain from naming your invoices at random. Unless you don't mind PDFs occasionally being replaced when chance produces the same numbers.

That's it, I suppose. Now, ideally, you can create cool PDFs with little effort.

Conclusion

We've tried to demonstrate an easy method for you to use Dompdf with PHP to convert HTML into PDF in this guide. The most common way to configure PHP to make PDFs is shown in our code example. Dompdf's capabilities can be readily increased by customizing parameters to match your needs. To get all necessary files, including the Dompdf library Save the source code.

Similar Tutorials

Comments