How to generate HTML to PDF in PHP using Dompdf library

  • Tech Area
  • March 10, 2023



In this tutorial, we have discuss how to generate HTML to PDF file in PHP using Dompdf library. Dompdf is a PHP library that provides a simple way to convert HTML to PDF documents. Using the Dompdf library you can easily generate PDF from the HTML page in PHP.

Below are the step by step process of how to generate HTML to PDF file in PHP using Dompdf library.

Step 1: Download Dompdf library

Click on below link to download Dompdf library and include it in the project directory.

https://github.com/dompdf/dompdf/releases

Step 2: Create index.php file

In this step, create a new file index.php. This is the main file used to generate HTML to PDF file using Dompdf library.

index.php

Include autoload.inc.php which inside the dompdf directory.

require_once 'dompdf/autoload.inc.php';

Define the Dompdf namespace

use Dompdf\Dompdf;

Create instance of the Dompdf class

$dompdf = new Dompdf();
// Load the HTML content 

$dompdf->loadHtml('<h1>Welcome to Tech Area</h1>'); 
 
// (Optional) Setup the paper size and orientation

$dompdf->setPaper('A4', 'landscape'); 
 
// Render the HTML as PDF

$dompdf->render(); 
 
// Output the generated PDF to Browser (1=download and 0=preview)

$dompdf->stream("techarea", array("Attachment"=>0));

Step 3: Create html-content.html file

Now, create a new file html-content.html. By the using of this HTML file you can easily generate a PDF file.

html-content.html

<!DOCTYPE html>
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

</body>
</html>

// Load content from the html file

$html = file_get_contents("html-content.html"); 
$dompdf->loadHtml($html); 

Source Code

Here is the full code that we have written for index.php.

<?php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$html = file_get_contents("html-content.html");
$dompdf->loadHtml($html);
// $dompdf->setPaper('A4','landscape');
$dompdf->render();
$dompdf->stream("techarea", array("Attachment"=>0));
?>

Download Source Code


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube