PHP & MySQL

DomPDF - Generate PDF from HTML with PHP

Guide to DomPDF - generate PDFs from HTML with PHP. Installation, CSS styling, page settings, headers/footers, and images.

What is DomPDF?

DomPDF is a PHP library that converts HTML and CSS into PDF documents. It renders HTML layouts using a CSS 2.1-compliant engine, making it ideal for generating invoices, reports, certificates, and any document where you want to define the layout with familiar web technologies.

Installation

# Install via Composer
composer require dompdf/dompdf

Quick Start

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

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);

$html = '<h1>Hello PDF</h1>
<p>Generated with DomPDF on ' . date('Y-m-d') . '</p>';

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// Output to browser
$dompdf->stream('document.pdf', [
    'Attachment' => false  // false = inline, true = download
]);

Styling with CSS

DomPDF supports most CSS 2.1 properties plus some CSS3 features. Inline styles and <style> blocks both work:

$html = '
<style>
  body { font-family: DejaVu Sans, sans-serif; }
  .header { background: #04122e; color: #fff; padding: 20px; }
  .header h1 { margin: 0; font-size: 24px; }
  table { width: 100%; border-collapse: collapse; margin-top: 20px; }
  th { background: #f2bf59; padding: 8px; text-align: left; }
  td { padding: 8px; border-bottom: 1px solid #ddd; }
  .total { font-weight: bold; font-size: 18px; text-align: right; }
</style>

<div class="header">
  <h1>Invoice #1042</h1>
</div>

<table>
  <thead><tr><th>Item</th><th>Qty</th><th>Price</th></tr></thead>
  <tbody>
    <tr><td>Widget A</td><td>3</td><td>$15.00</td></tr>
    <tr><td>Widget B</td><td>1</td><td>$42.00</td></tr>
  </tbody>
</table>
<p class="total">Total: $87.00</p>';

$dompdf->loadHtml($html);

Paper Sizes & Orientation

SizeDimensionsCommon Use
A4210 × 297 mmInternational standard
Letter8.5 × 11 inUS standard
Legal8.5 × 14 inContracts
Custom[0, 0, w, h] in pointsLabels, receipts
// Standard sizes
$dompdf->setPaper('A4', 'portrait');
$dompdf->setPaper('letter', 'landscape');

// Custom size (4 × 6 inches in points: 1 inch = 72 points)
$dompdf->setPaper([0, 0, 288, 432]);

Headers & Footers with Page Numbers

$dompdf->render();

// Access the canvas to draw on every page
$canvas = $dompdf->getCanvas();
$canvas->page_text(
    36, 18,          // x, y position
    'My Company',    // text
    null, 10, [0,0,0]  // font, size, color
);

// Page numbers in footer
$canvas->page_text(
    500, 780,
    'Page {PAGE_NUM} of {PAGE_COUNT}',
    null, 9, [0.5, 0.5, 0.5]
);

Loading External Files

// Load from a URL
$dompdf->loadHtmlFile('https://example.com/report.html');

// Load from a local file
$dompdf->loadHtmlFile('/var/www/templates/invoice.html');

// Include images (requires isRemoteEnabled = true)
$html = '<img src="https://example.com/logo.png" width="200">';

// Or embed base64 images
$data = base64_encode(file_get_contents('logo.png'));
$html = "<img src='data:image/png;base64,$data'>";

Save to File Instead of Streaming

$dompdf->render();
$output = $dompdf->output();
file_put_contents('/path/to/output.pdf', $output);

Security Considerations

When generating PDFs from user-supplied HTML, always sanitize input to prevent script injection and file disclosure:

$options = new Options();
$options->set('isPhpEnabled', false);      // DISABLE PHP in templates
$options->set('isRemoteEnabled', false);   // Block remote resources
$options->set('isJavascriptEnabled', false);

// Whitelist specific fonts directory
$options->set('fontDir', '/var/fonts');
$options->set('fontCache', '/tmp/dompdf-cache');

CSS Support Reference

SupportedPartial / LimitedNot Supported
margin, padding, borderfloat (basic)flexbox
font-family, font-size, colorposition: absolutegrid
background-color, background-image@page (margins)CSS animations
text-align, line-heightopacitytransforms
table layoutborder-radiusmedia queries

Last updated: 2026 • Browse all courses