Dompdf keeps a white border around the PDF whatever margin I set
Your error
The PDF keeps a border of about 1.2 cm even with body { margin: 0 }2 more lines it can showFix it fast
Most common cause, 1 of 3The margin is set on body or a wrapper, not on the page
Add an @page rule with the margins you want, in a style block of the HTML you pass to loadHtml, and reset body if it carries its own margin.
<style>
@page { margin: 10mm 12mm; }
body { margin: 0; }
</style>Short answer
Dompdf keeps its own page margin of 1.2cm from its built-in stylesheet until an @page rule replaces it, so margins on body or a wrapper only add space inside that border. Put @page { margin: 10mm; } in a style block Dompdf reads. Rules inside @media print are skipped, because Dompdf renders with the screen media type by default.
Which cause do you have?
Render a one-line test whose only CSS is an @page rule in a style block. If its margin changes, Dompdf handles @page fine and the problem is where your real rule lives or how it is loaded.
php -r 'require "vendor/autoload.php"; $d = new Dompdf\Dompdf(); $d->loadHtml("<style>@page { margin: 0 }</style><p>edge test</p>"); $d->render(); file_put_contents("edge-test.pdf", $d->output());'Then find the line you see in this list and open its fix:
- 1The margin is set on body or a wrapper, not on the page
The CSS has body { margin: 0 } or a padded container, and no @page rule at all - 2The @page rule sits inside @media print
The rule is wrapped in @media print { ... } or lives in a stylesheet linked with media="print" - 3The stylesheet holding @page is never loaded
Fonts and colours from the same CSS file are missing as well, and Dompdf's warnings list Unable to load css file or Permission denied on the file
What does the error look like?
The PDF keeps a border of about 1.2 cm even with body { margin: 0 }@page { margin: 0 } changes nothing in the Dompdf outputUnable to load css file
Cause 1 of 3
The margin is set on body or a wrapper, not on the page
You seeThe CSS has body { margin: 0 } or a padded container, and no @page rule at all
Check
Dompdf's own stylesheet opens with @page { margin: 1.2cm; }, and Dompdf places the content of every page inside the margins of that page style. Margins on body, or padding on a wrapper div, sit inside the page box, so they add space inside that border instead of replacing it.
grep -rn --exclude-dir=vendor '@page' .Fix
Add an @page rule with the margins you want, in a style block of the HTML you pass to loadHtml, and reset body if it carries its own margin.
<style>
@page { margin: 10mm 12mm; }
body { margin: 0; }
</style>Verify
Render again; the text now starts the set distance from the paper edge.
php render-invoice.php # your script that writes invoice.pdfConfirmed by github.com
Cause 2 of 3
The @page rule sits inside @media print
You seeThe rule is wrapped in @media print { ... } or lives in a stylesheet linked with media="print"
Check
Dompdf always applies rules for the media types all, static, visual, bitmap, paged and dompdf, plus one more taken from its defaultMediaType option, and that option is screen unless you change it. A print-only @page rule is therefore dropped, and the default 1.2 cm stays.
grep -rn --exclude-dir=vendor -B3 '@page' . | grep '@media'Fix
Move the @page rule out of the print block, or tell Dompdf to render as print media.
$options = $dompdf->getOptions();
$options->setDefaultMediaType('print');
$dompdf->setOptions($options);Verify
The margins from the print block now appear in the rendered PDF.
php render-invoice.php # rerun after the media type changeConfirmed by github.com
Cause 3 of 3
The stylesheet holding @page is never loaded
You seeFonts and colours from the same CSS file are missing as well, and Dompdf's warnings list Unable to load css file or Permission denied on the file
Check
Dompdf loads a linked stylesheet only when its resource rules allow it: an http or https URL needs isRemoteEnabled set to true and either curl or allow_url_fopen, and a local file must lie inside the chroot paths. When a check fails, Dompdf records a warning and skips the whole file, @page rule included.
// after $dompdf->render():
print_r($GLOBALS['_dompdf_warnings'] ?? []);Fix
Inline the page CSS in a style block, or allow the location the stylesheet comes from. Enable remote loading only for URLs you control.
$options = $dompdf->getOptions();
$options->setChroot([__DIR__ . '/public']); // local CSS under public/
// $options->setIsRemoteEnabled(true); // trusted http(s) CSS only
$dompdf->setOptions($options);Verify
The warnings list is empty, and the margins, fonts and colours from the file show in the PDF.
print_r($GLOBALS['_dompdf_warnings'] ?? []);Confirmed by github.com
How do I stop it coming back?
- Keep the @page rule in a style block inside the PDF template itself, so a moved or blocked stylesheet cannot reset the margins.
- Write page margins only in @page and leave body at margin 0, so there is one place to change them.
- Print Dompdf's warning list in a test that renders each template, and fail the test when it is not empty.
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.