Dompdf is an HTML to PDF converter for PHP. It's a style-driven renderer that understands HTML and CSS and converts them into a PDF document. It's particularly useful for generating reports, invoices, certificates, or any web content that needs to be presented in a print-ready PDF format.
Key Features:
* HTML & CSS Support: Dompdf aims to support a wide range of HTML 4.01 (and some HTML5) and CSS2.1 (and some CSS3) specifications, allowing developers to design PDFs using familiar web technologies.
* Image Support: Supports various image formats including GIF, PNG, JPG, and SVG (with limitations).
* Custom Fonts: Allows embedding custom fonts for branding consistency.
* Table and List Rendering: Handles complex table structures and ordered/unordered lists.
* Page Breaking and Numbering: Provides mechanisms to control page breaks and add page numbers.
* Flexible Output: Can stream PDFs directly to the browser for viewing or save them to a file on the server.
How it Works:
1. Parsing: Dompdf takes HTML and CSS as input.
2. Rendering: It parses the HTML into a DOM tree and applies the CSS styles to create a visual representation.
3. PDF Generation: This visual representation is then converted into a PDF document using PHP's PDF rendering capabilities, leveraging various external libraries and its own rendering engine.
Installation:
Dompdf is typically installed via Composer, the PHP dependency manager:
```bash
composer require dompdf/dompdf
```
Usage:
1. Include the autoloader from Composer.
2. Instantiate the `Dompdf` class.
3. Load HTML content into the instance.
4. (Optional) Configure paper size, orientation, and other options.
5. Call the `render()` method to generate the PDF.
6. Stream the PDF to the browser using `stream()` or save it to a file using `output()` and file system functions.
Example Code
<?php
require_once 'vendor/autoload.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// (Optional) Configure options if needed
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true); // Enable remote asset loading (e.g., images from URLs)
$dompdf->setOptions($options);
// HTML content to convert
$html = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dompdf Example PDF</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
}
h1 {
color: #333366;
text-align: center;
}
p {
line-height: 1.6;
}
.container {
border: 1px solid #ddd;
padding: 20px;
margin-top: 20px;
background-color: #f9f9f9;
}
.footer {
text-align: center;
margin-top: 30px;
font-size: 0.8em;
color: #777;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #e0e0e0;
}
</style>
</head>
<body>
<h1>Welcome to Dompdf</h1>
<div class="container">
<p>This is an example of generating a PDF document using the Dompdf PHP library. You can include various HTML elements and styles.</p>
<p>Dompdf allows you to convert complex HTML pages, including CSS styling, into a printable PDF format. This is extremely useful for generating reports, invoices, certificates, or any document that needs a fixed layout for printing or archiving.</p>
<h2>Example Table</h2>
<table>
<thead>
<tr>
<th>Item</th>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Product A</td>
<td>A high-quality product.</td>
<td>1</td>
<td>$100.00</td>
</tr>
<tr>
<td>Service B</td>
<td>Professional service.</td>
<td>2</td>
<td>$50.00</td>
</tr>
<tr>
<td colspan="3" style="text-align: right;"><strong>Total:</strong></td>
<td><strong>$200.00</strong></td>
</tr>
</tbody>
</table>
<h3>Key Benefits:</h3>
<ul>
<li>Easy to use with familiar HTML/CSS.</li>
<li>Generates high-quality PDF output.</li>
<li>Supports images and custom fonts.</li>
</ul>
</div>
<div class="footer">
Generated on ' . date('Y-m-d H:i:s') . ' by Dompdf.
</div>
</body>
</html>';
$dompdf->loadHtml($html);
// (Optional) Set paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
// You can specify the filename in the stream method
$dompdf->stream("dompdf_example.pdf", ["Attachment" => false]); // true for download, false for inline view
// To save the PDF to a file on the server:
// file_put_contents(__DIR__ . '/output/generated_document.pdf', $dompdf->output());
?>








dompdf/dompdf