HTML to PDF in PHP Using DomPDF

In this comprehensive guide, we'll explore how to create high-quality PDF documents from HTML using DomPDF. As businesses increasingly need to generate professional documents such as invoices, reports, and certificates programmatically, mastering HTML to PDF conversion becomes an essential skill. This step-by-step tutorial will walk you through setting up DomPDF in your PHP project, designing templates, and converting them into polished PDFs effortlessly. Discover how DomPDF can transform your document generation process while maintaining complete control over the output.
What is DomPDF?
DomPDF is a PHP library that converts HTML and CSS to PDF. It's a style-driven renderer that parses your content and generates PDF documents based on the HTML structure and CSS styling you provide. Unlike other PDF generation approaches that require learning proprietary APIs, DomPDF allows you to leverage your existing HTML and CSS knowledge to create professional documents.
Key features of DomPDF:
- Extensive CSS 2.1 and partial CSS3 support.
- Relatively small footprint with minimal dependencies.
- Support for various image formats (PNG, JPG, GIF).
- UTF-8 character encoding for international text.
- Table, header, and footer support.
- Customizable page size and orientation.
- Support for custom fonts.
- Open-source and free to use.
Getting Started with DomPDF
Before diving into complex invoice generation, let's start with the basics to understand how DomPDF works. In this section, we'll set up a simple PDF generation script.
Prerequisites
Ensure your PHP environment is properly configured for PDF generation.
| Requirement | Recommendation and Details |
|---|---|
| PHP Version | PHP 7.1 or higher with the following extensions: gd, mbstring, dom, xml. |
| Composer | PHP dependency manager – Get Composer. |
| IDE | PHP-friendly IDE like PHPStorm, VS Code with PHP extensions, or NetBeans. |
Basic Setup
- First, create a new project directory and navigate into it:
mkdir dompdf-project
cd dompdf-project
- Next initialize a new Composer project:
composer init --no-interaction --name=your-name/dompdf-project
- Install DomPDF via Composer:
composer require dompdf/dompdf
Simple Example
Let's create a simple PHP script to generate a basic PDF with a title, some content and a footer to demonstrate the fundamental DomPDF workflow.
View Code
<?php
// Require the Composer autoloader
require 'vendor/autoload.php';
// Import DomPDF classes
use Dompdf\Dompdf;
use Dompdf\Options;
// Configure DomPDF options
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
// Create a new DomPDF instance
$dompdf = new Dompdf($options);
// Simple HTML content
$html = '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple PDF Example</title>
<style>
body {
margin: 30px;
}
h1 {
color: #7f4184;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
.content {
margin-top: 20px;
}
.footer {
margin-top: 50px;
font-size: 16px;
color: #4f417c;
text-align: center;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Hello!</h1>
<div class="content">
<h2>This is a simple PDF document generated using DomPDF.</h2>
<p>Current date and time: ' . date('Y-m-d H:i:s') . '</p>
</div>
<div class="footer">
Generated with DomPDF
</div>
</body>
</html>
';
// Load HTML content
$dompdf->loadHtml($html);
// Set paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to browser (inline viewing)
$dompdf->stream("document.pdf", [
"Attachment" => false // Set to true for forced download
]);
