Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

The 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.

Fix
<style>
  @page { margin: 10mm 12mm; }
  body  { margin: 0; }
</style>
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Dompdf 3.1 (3.1.6), PHP 8.2 to 8.6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1The margin is set on body or a wrapper, not on the pageThe CSS has body { margin: 0 } or a padded container, and no @page rule at all
  2. 2The @page rule sits inside @media printThe rule is wrapped in @media print { ... } or lives in a stylesheet linked with media="print"
  3. 3The stylesheet holding @page is never loadedFonts 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 output
  • Unable 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.

Check
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.

Fix
<style>
  @page { margin: 10mm 12mm; }
  body  { margin: 0; }
</style>

Verify

Render again; the text now starts the set distance from the paper edge.

Verify
php render-invoice.php   # your script that writes invoice.pdf

Confirmed by github.com

Did this fix it?

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.

Check
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.

Fix
$options = $dompdf->getOptions();
$options->setDefaultMediaType('print');
$dompdf->setOptions($options);

Verify

The margins from the print block now appear in the rendered PDF.

Verify
php render-invoice.php   # rerun after the media type change

Confirmed by github.com

Did this fix it?

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.

Check
// 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.

Fix
$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.

Verify
print_r($GLOBALS['_dompdf_warnings'] ?? []);

Confirmed by github.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.