WhatsApp us

How to generate HTML to PDF in PHP using Dompdf library

  • Tech Area
  • Last updated on: January 8, 2026



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 20,000+ subscriber

Subscribe on YouTube

PHP Projects
Matrimonial Portal Project in PHP & MySQL Last Updated: December 22, 2025
Event Management System Project in PHP & MySQL Last Updated: December 6, 2025
Online Shopping System Project in PHP MySQL Last Updated: November 26, 2025
Hostel management system project in PHP and MySQL Last Updated: February 14, 2024
Online Pizza Delivery project in PHP Last Updated: February 4, 2024
Parking Management System project in PHP Last Updated: November 5, 2023
Visitors Management System project in PHP Last Updated: August 28, 2023
SNIPE – IT Asset management system v6.1.0 Last Updated: April 21, 2023
Employee Management System project in PHP Last Updated: January 21, 2023