Dompdf renders the HTML but ignores the layout or the whole stylesheet
Your error
Columns that sit side by side in the browser are stacked in the PDF3 more lines it can showFix it fast
Most common cause, 1 of 2The layout depends on flexbox or CSS grid
Build side-by-side parts of the PDF template with a table, which Dompdf supports well, including colspan, rowspan and both border models.
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="width: 50%; vertical-align: top;">Billed to: ...</td>
<td style="width: 50%; vertical-align: top; text-align: right;">Invoice #1042</td>
</tr>
</table>Short answer
When CSS works in the browser but not in Dompdf, check two things. Dompdf handles most of CSS 2.1 and only some CSS3, with no flexbox or grid, so a Bootstrap grid collapses into one column; rebuild such parts with tables. A linked stylesheet outside the chroot, on a remote host or sent without text/css is dropped. Applies to Dompdf 3.1 on PHP 8.2-8.6.
Which cause do you have?
Render once and print the warnings Dompdf collected. An empty list while only the layout is wrong means cause 1. Any Error loading or Unable to load css file line means the stylesheet never reached Dompdf, which is cause 2.
$dompdf->loadHtml($html);
$dompdf->render();
print_r($GLOBALS['_dompdf_warnings'] ?? []);Then find the line you see in this list and open its fix:
- 1The layout depends on flexbox or CSS grid
No warnings; colours and fonts apply, but rows of cards, Bootstrap columns or a grid of items stack vertically - 2The linked stylesheet is rejected before parsing
The warnings contain Error loading ... Options::chroot, Remote file requested, but remote file download is disabled, Remote host is not in allowed list, or Unable to load css file
What does the error look like?
Columns that sit side by side in the browser are stacked in the PDFError loading /var/www/app/public/css/pdf.css: Permission denied. The file could not be found under the paths specified by Options::chroot.Error loading https://cdn.example.com/app.css: Remote file requested, but remote file download is disabled.Unable to load css file https://example.com/pdf-styles
Cause 1 of 2
The layout depends on flexbox or CSS grid
You seeNo warnings; colours and fonts apply, but rows of cards, Bootstrap columns or a grid of items stack vertically
Check
CSS that lays out boxes with display: flex or display: grid does nothing in Dompdf. Its README lists support for most CSS 2.1 and a few CSS3 properties, and names flexbox and CSS Grid among the known limitations. Bootstrap 5 builds its grid of rows and columns with flexbox, so a template that reuses the site's framework classes falls back to block boxes, one under the other.
grep -rnE "display: ?(flex|grid)|d-flex|class=\"row" resources/views/pdf/Fix
Build side-by-side parts of the PDF template with a table, which Dompdf supports well, including colspan, rowspan and both border models. Keep a separate, plain stylesheet for the PDF instead of the site's framework CSS.
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="width: 50%; vertical-align: top;">Billed to: ...</td>
<td style="width: 50%; vertical-align: top; text-align: right;">Invoice #1042</td>
</tr>
</table>Verify
Render again: the two blocks sit next to each other in the PDF, just as in the browser.
file_put_contents('layout-test.pdf', $dompdf->output());Confirmed by raw.githubusercontent.com
Cause 2 of 2
The linked stylesheet is rejected before parsing
You seeThe warnings contain Error loading ... Options::chroot, Remote file requested, but remote file download is disabled, Remote host is not in allowed list, or Unable to load css file
Check
Dompdf drops a <link> stylesheet as a whole when its own resource rules refuse it. A local file has to resolve inside the chroot, which defaults to the Dompdf package folder; an http or https URL needs isRemoteEnabled, which is false by default, and can be limited by allowedRemoteHosts; and a remote response whose Content-Type is not text/css is refused with Unable to load css file.
grep -rn "<link" resources/views/pdf/Fix
The sturdiest fix is to read the CSS in PHP and put it in a style block, so no file or network check is involved. If the link must stay, widen the chroot to the folder that holds it, or allow only the one remote host that serves it.
// inline the PDF stylesheet
$css = file_get_contents(__DIR__ . '/../public/css/pdf.css');
$dompdf->loadHtml('<style>' . $css . '</style>' . $body);
// or keep the link and allow its location
$options = $dompdf->getOptions();
$options->setChroot([realpath(__DIR__ . '/../public')]);
// $options->setIsRemoteEnabled(true);
// $options->setAllowedRemoteHosts(['cdn.example.com']);
$dompdf->setOptions($options);Verify
The warnings list comes back empty after render, and the PDF shows the stylesheet's fonts, colours and spacing.
print_r($GLOBALS['_dompdf_warnings'] ?? []);Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Keep one small PDF stylesheet without a CSS framework and design invoice and report templates with tables from the start.
- Render every PDF template in a test and fail it when $GLOBALS['_dompdf_warnings'] is not empty.
- Turn remote loading on only together with an allowedRemoteHosts list, and prefer inlined CSS for anything the PDF needs.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.