PHP HTML to PDF: When Dompdf Breaks Your Flexbox
Updated by Cyril Nicodème
If your invoice's totals come out of Dompdf as "Subtotal$6,600.00", with the label and the amount touching, the row is a flexbox and Dompdf doesn't do flexbox. mPDF printed ours the same way. For one fixed template you own, three lines of table CSS fix it in Dompdf. Once your PDFs share your app's CSS (flexbox, grid, a script), stop rewriting them for Dompdf and send that HTML to Chromium. One curl call to PDFShift does that, with nothing to install on your server.


How do you convert HTML to PDF in PHP with Dompdf?
Dompdf is a PHP library that turns HTML and CSS into a PDF with its own layout engine. There's no browser and no binary to install: composer require dompdf/dompdf, then:
<?php
// Dompdf 3.1.6, https://github.com/dompdf/dompdf
require __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf(['isRemoteEnabled' => true]);
$dompdf->loadHtml(file_get_contents(__DIR__ . '/invoice.html'));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
file_put_contents(__DIR__ . '/invoice.pdf', $dompdf->output());
On our invoice of 40 line items it took 0.06 seconds and wrote a 6 KB file of 3 pages, with the table's header row repeated on each one. It got one thing wrong: the totals box, three flexbox rows with justify-content: space-between, came out as one run of text per row.
That's Dompdf working as designed. Its README says it handles "most CSS 2.1 and a few CSS3 properties", and it lists what it doesn't do: no flexbox (issue #971), no grid (#2988), and a table row must fit on a single page (#98). The inline script in our invoice didn't run either. Fonts take work too: for any character outside Windows ANSI, the README says you must supply an external font.
The fix: lay the row out as a table
We left the HTML alone and added three rules that turn each totals row into a two-cell table, a CSS 2.1 layout:
.totals div { display: table; width: 100%; }
.totals span { display: table-cell; }
.totals span + span { text-align: right; }
Dompdf then printed "Subtotal" on the left and "$6,600.00" on the right, same 3 pages, in 0.08 seconds. The same rules did nothing in mPDF 8.3.2, where the totals stayed jammed.
That's the trade. A fix like this is fine for one invoice that rarely changes. But every flex or grid layout in the template needs its own rewrite, and from then on your PDF template is a second copy of your layout, written in older CSS, that drifts from your app's stylesheet. If your designers work in Tailwind or your charts draw with a script, you'll keep paying that price.
mPDF is the other PHP-only library. It also skips flexbox and scripts, and it's a bigger install: 93 MB in vendor/, 87 MB of which are fonts. It's licensed GPL-2.0-only, so check it with whoever signs off on your licences.
Which PHP HTML to PDF package uses which engine?
The package name rarely says what renders your HTML.
| Package | Engine | Flexbox and grid | Runs JavaScript | Adds to your server |
|---|---|---|---|---|
| PDFShift API (PHP's curl) | Chromium | Yes | Yes | Nothing |
| spatie/browsershot | Chrome through Puppeteer | Yes | Yes | Node 22+, Puppeteer, Chrome |
| chrome-php/chrome (not tested) | Chrome, driven from PHP | Yes, it's Chrome | Yes, it's Chrome | A Chrome or Chromium 65+ binary |
| dompdf/dompdf | Its own CSS 2.1 engine | No | No | 9.7 MB of PHP |
| mpdf/mpdf | Its own engine | No (our test) | No | 93 MB of PHP and fonts |
| knplabs/snappy | wkhtmltopdf, archived in 2023 | Not tested | Not tested | The wkhtmltopdf binary |
Snappy runs wkhtmltopdf, which won't get fixes: its GitHub repository has been archived and read-only since January 2, 2023. TCPDF is a different kind of tool: you build the PDF with PHP calls instead of HTML, and its own README says it's in maintenance-only mode, with new work going into tc-lib-pdf.
HTML to PDF in Laravel: your composer.json already picked the engine
Most Laravel apps reach these engines through a wrapper, so check composer.json before you blame your CSS:
- barryvdh/laravel-dompdf (3.1.2) runs Dompdf. Everything in the Dompdf section above applies, including the three-line table fix for your flexbox rows.
- barryvdh/laravel-snappy runs Snappy, so wkhtmltopdf, and you download that binary yourself and keep it on every server.
- spatie/laravel-pdf (version 2) runs the driver you configure, Browsershot by default. Its other drivers are Gotenberg, Cloudflare, WeasyPrint, Dompdf and Chrome. Spatie's docs say the Browsershot, Gotenberg, Cloudflare and Chrome drivers handle grid and flexbox. They call the Dompdf driver "ideal for simpler PDFs that don't need advanced layout features."
Keep your CSS: PHP HTML to PDF with Chrome
Spatie's Browsershot sends your HTML to Chrome through Puppeteer and saves what Chrome prints. composer require spatie/browsershot, npm install puppeteer, then:
<?php
// spatie/browsershot 5.5.2 with Puppeteer 26.0.0, https://spatie.be/docs/browsershot/v5
require __DIR__ . '/vendor/autoload.php';
use Spatie\Browsershot\Browsershot;
Browsershot::html(file_get_contents(__DIR__ . '/invoice.html'))
->format('A4')
->showBackground()
->save(__DIR__ . '/invoice.pdf');
The PDF came out right: each total's label and amount apart, header row repeated, the script ran, 3 pages, 121 KB. Each call took about 0.7 seconds on our Mac, starting and closing its own Chrome.
The cost is what it puts on your server. Browsershot 5 needs Node 22 or newer and Puppeteer 23 or newer, and Puppeteer brings its own Chrome. On our Mac that was 33 MB of node_modules and a 390 MB Chrome download. npm 11 skipped Puppeteer's install script by default, so Chrome didn't download until we ran npx puppeteer browsers install chrome. That's a step to remember in every deploy. chrome-php/chrome drives Chrome straight from PHP without Node, but you still install and update the Chrome binary yourself.
HTML to PDF API in PHP with curl
PDFShift renders your HTML in Chromium on our servers, so the CSS and scripts your app already uses come out as they do in Chrome, and nothing gets installed on yours. It's one POST with PHP's curl extension:
<?php
// PDFShift API v3, https://docs.pdfshift.io/
$ch = curl_init('https://api.pdfshift.io/v3/convert/pdf');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-Key: ' . getenv('PDFSHIFT_API_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'source' => file_get_contents(__DIR__ . '/invoice.html'),
'format' => 'A4',
'use_print' => true,
]),
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
throw new RuntimeException('PDFShift error: ' . $pdf);
}
file_put_contents(__DIR__ . '/invoice.pdf', $pdf);
source takes your HTML or a URL, and use_print renders with your print CSS. We got the same result as Browsershot: 3 pages, the header row repeated, each total's label and amount apart, the script ran, a 35 KB file. Each request took 2.6 to 3.1 seconds from our machine, network round trip included. A document up to 5 MB costs 1 credit.
The same request takes more options, each one more key in that JSON body. Add header to print "Page 2 of 3" on every page, wait_for to hold the conversion until your charts finish drawing, watermark to stamp the PDF with text, protection to set a user and owner password, or webhook to get a POST on your server when a conversion is done, so a big batch doesn't keep your PHP workers waiting.
We've run conversions since 2018, on servers in France, and we don't store your documents by default. PDFShift holds 4.8 stars from 65 reviews on Capterra. Create a free account for 50 credits a month, no card needed. The same call with custom headers, cookies or an S3 upload is in our PHP curl guides.
Frequently asked questions
Can I convert HTML to PDF in PHP without installing anything?
Yes. A request to an HTML-to-PDF API like PDFShift needs only PHP's curl extension. Dompdf needs only Composer, but it renders CSS 2.1, so check your template against its limits first.
Which PHP library supports flexbox and grid?
None of the PHP-only libraries in our test: Dompdf's README says it doesn't support flexbox or grid, and mPDF printed our flexbox rows jammed. Routes that use Chrome or Chromium support both: Browsershot, chrome-php/chrome and the PDFShift API.
What is the best HTML to PDF converter for PHP?
PDFShift, once your PDFs use your app's own CSS: flexbox, grid and scripts come out as they do in Chrome, with nothing installed on your server. For one template written in CSS 2.1 that you control, Dompdf is fast, small and needs only Composer. If you're choosing in another language, we ran the same kind of test for Python and Java.
If one of these snippets doesn't work on your setup,
tell us and we'll take a look.