DomPDF for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to DomPDF: What It Is and Why You Need It

\n\n

DomPDF is a powerful and popular library for the PHP language, designed for generating PDF documents from HTML markup. The main idea of the library is to allow developers to create PDF files using familiar web layout technologies: HTML, CSS, and JavaScript (limited). Instead of learning complex low-level APIs for working with PDF (like in FPDF or TCPDF), you can simply write an HTML template, style it with CSS, and pass it to DomPDF for conversion.

\n\n

The library uses a rendering engine based on DOMPDF (hence the name), which parses HTML and CSS, creates a visual representation of the page, and then exports it to PDF format. DomPDF supports most standard CSS properties, including positioning, float, flexbox (partially), working with fonts, images, and tables.

\n\n

Why do you need this library?

\n
    \n
  • Report automation: Creating invoices, acts, waybills, commercial offers based on data from a database.
  • \n
  • Certificate and diploma generation: Quick export of personalized documents with unique data (name, date, course).
  • \n
  • Creating e-books and manuals: Converting HTML content into a well-formatted PDF for download.
  • \n
  • Saving web pages: "Save as PDF" functionality for any content on the site.
  • \n
  • Integration with CMS: WordPress, Laravel, Symfony, and other frameworks actively use DomPDF for document generation.
  • \n
\n\n

The main advantage of DomPDF over alternatives is its ease of use. If you know HTML and CSS, you already know 80% of how to work with DomPDF.

\n\n

Installing DomPDF

\n\n

Installing DomPDF in a modern PHP project is done via the dependency manager Composer. This is the standard method for PHP libraries.

\n\n

Make sure you have Composer installed. Then run the command in the root of your project:

\n\n
composer require dompdf/dompdf
\n\n

After successful installation, a vendor folder will appear in your project, and a dependency line will be added to the composer.json file. If you are using a framework (Laravel, Symfony), the installation may differ slightly (e.g., through service providers), but the basic command remains the same.

\n\n

Important: DomPDF requires PHP version 7.1 or higher. It is also recommended to install the PHP extension mbstring for correct work with multibyte encodings (UTF-8).

\n\n

To verify the installation, create a file test.php and add the following code to it:

\n\n
<?php\nrequire_once 'vendor/autoload.php'; // Include the Composer autoloader\n\nuse Dompdf\\Dompdf;\n\n$dompdf = new Dompdf();\n$dompdf->loadHtml('<h1>Hello World</h1>');\n$dompdf->render();\n$dompdf->stream("test.pdf");\n\n?>
\n\n

If opening the file in your browser downloads a PDF with the text "Hello World", the installation was successful.

\n\n

Quick Start: Your First PDF Document

\n\n

Let's create a simple but complete example. We will generate a PDF file with a table and styles.

\n\n
<?php\nrequire_once 'vendor/autoload.php';\n\nuse Dompdf\\Dompdf;\nuse Dompdf\\Options;\n\n// 1. Configure options (optional)\n$options = new Options();\n$options->set('defaultFont', 'DejaVu Sans'); // Font with Cyrillic support\n$options->set('isRemoteEnabled', true); // Allow loading external images\n\n// 2. Create a Dompdf instance\n$dompdf = new Dompdf($options);\n\n// 3. HTML content\n$html = '\n<!DOCTYPE html>\n<html>\n<head>\n    <style>\n        body { font-family: DejaVu Sans, sans-                    

Recommendations