All formats

Dompdf

LanguagePHP 7.1+
LicenseLGPL-2.1
Repositorygithub.com/dompdf/dompdf
DocumentationREADME and wiki
Depends onext-dom, ext-mbstring, php-font-lib, php-svg-lib, masterminds/html5; ext-gd for images
Read / writeno / yes

Dompdf is an HTML-to-PDF renderer written in PHP: it parses HTML5 and CSS 2.1 (with some CSS3, such as @font-face, @page, border-radius and transforms), lays out the document and writes PDF through its bundled CPDF backend. It’s the usual choice for turning Blade or Twig templates (invoices, tickets, reports) into PDFs, and Laravel’s barryvdh/laravel-dompdf wraps it.

Its layout engine predates flexbox and grid, so use tables and floats for layout. Large documents are slow and memory-hungry. Dompdf only creates PDFs; it can’t read or merge existing ones.

Installation#

Shell
composer require dompdf/dompdf

Basic usage: convert HTML to PDF#

PHP
<?php
require 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('isRemoteEnabled', false);      // don't fetch remote images or CSS
$options->set('defaultFont', 'DejaVu Sans');  // bundled font with wide Unicode coverage

$dompdf = new Dompdf($options);
$html = '<html><head><style>
  body { font-family: "DejaVu Sans", sans-serif; font-size: 12px; }
  table { border-collapse: collapse; width: 100%; }
  td, th { border: 1px solid #999; padding: 4px; }
  @page { margin: 2cm; }
</style></head><body>
  <h1>Invoice #1001</h1>
  <p>Zażółć gęślą jaźń · Привет</p>
  <table><tr><th>Item</th><th>Price</th></tr><tr><td>Widget</td><td>9.99</td></tr></table>
</body></html>';

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
file_put_contents('invoice.pdf', $dompdf->output());

In a web app, $dompdf->stream('invoice.pdf') sends the file to the browser.

How to add page numbers#

Draw on the canvas after render(); {PAGE_NUM} and {PAGE_COUNT} are replaced on every page:

PHP
$dompdf->render();
$canvas = $dompdf->getCanvas();
$canvas->page_text(520, 800, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 9, [0, 0, 0]);

Coordinates are in points from the top-left corner.

How to set PDF metadata#

PHP
$dompdf->render();
$dompdf->addInfo('Title', 'Invoice #1001');
$dompdf->addInfo('Author', 'Billing service');

A <title> element in the HTML also sets the document title.

How to use custom fonts#

Declare the font with @font-face; Dompdf converts and caches it in its font directory (it must be writable):

CSS
@font-face {
  font-family: "Inter";
  src: url("fonts/Inter-Regular.ttf") format("truetype");
}
body { font-family: "Inter", sans-serif; }

Set the chroot option to the folder that contains your local fonts and images.

How to password-protect the PDF#

The CPDF backend supports only RC4 40-bit encryption (revision 2):

PHP
$dompdf->render();
$dompdf->getCanvas()->get_cpdf()->setEncryption('user', 'owner', ['print']);
file_put_contents('secret.pdf', $dompdf->output());

This keeps casual users out but is easy to break; if you need AES-256, encrypt the output with qpdf or pikepdf.

Tips#