Use the URL to PDF Converter when you want the file without installing anything. The steps below are the detail.
Dompdf converts HTML to PDF inside PHP. It is not Chrome. The project describes itself as a style-driven renderer that handles most of CSS 2.1 and a few CSS3 properties, including @import, @media, and @page. Install it with Composer, pass an HTML string, and write the PDF to disk.
Install Dompdf
You need PHP with the DOM and MBString extensions. The current package line is 3.x. Check the PHP version on the package page before you install, then run:
composer require dompdf/dompdf
Load Composer’s autoloader before you use the class. A packaged zip ships autoload.inc.php instead. The snippets below use Composer.
Convert an HTML string
loadHtml takes the markup, setPaper picks the page, render lays it out, and output returns the PDF bytes. stream sends those bytes to the browser. This script saves a file.
<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: "DejaVu Sans", sans-serif; font-size: 12pt; }
h1 { font-size: 18pt; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #999; padding: 6px; }
tr { page-break-inside: avoid; }
</style>
</head>
<body>
<h1>October report</h1>
<p>Generated with Dompdf.</p>
</body>
</html>
HTML;
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
file_put_contents('report.pdf', $dompdf->output());
Use one Dompdf instance for one document. The project warns that a second render on the same object can keep state from the first.
Fonts and remote images
The built-in PDF fonts (Helvetica, Times, Courier) only cover Windows ANSI. DejaVu Sans, DejaVu Serif, and DejaVu Sans Mono are already installed. Name one in CSS, as the sample does, or set the default:
use Dompdf\Options;
$options = new Options();
$options->set('defaultFont', 'DejaVu Sans');
$dompdf = new Dompdf($options);
A remote image is blocked until isRemoteEnabled is true, and PHP needs cURL or allow_url_fopen. Leave remote access off unless the HTML really loads files over HTTP. A local image has to sit inside the chroot path. Set that path to the directory you trust, not to the whole disk:
$options->set('chroot', __DIR__);
CSS that Dompdf will not render
Flexbox is unsupported. CSS Grid is unsupported. A table row has to fit on one page, because a cell is not split across pages. Raw inline SVG is unreliable. Link an SVG file, or put the markup in an img data URI.
Dompdf does read @page and @media. That is Dompdf, not a browser engine. A stylesheet that looks right in Chrome can still move in the PDF. Keep layout in tables, blocks, and floats. For the print-CSS properties that do work in a browser, see print from CSS to PDF.
mPDF is the other common PHP library. It has its own HTML subset and its own header API. It is not a second way to call Dompdf. Older WebKit tools such as wkhtmltopdf are a different problem, covered in the wkhtmltopdf alternative.
Skip the install
If you only need one public page as a PDF, the URL to PDF converter renders it with headless Chrome. That converter uses screen media, skips rules that apply only to print, and takes the paper size from the form rather than from @page. The file is deleted after the download. There is no account and no watermark.
Python and Node versions of the same job: HTML to PDF in Python and HTML to PDF in Node.





