50

I'm trying to generate a PDF using DOMPDF. I have some HTML which is then converted into a PDF.

But I have a problem. When I put an object at the top of the page (e.g. an icon), in the PDF it's also show on the top of the page. But when I print the PDF there is a margin. I know that there has to be a minimal margin, but in my case it's just too large. Is there some way to control this margin in DOMPDF?

I'm trying to reproduce an existing document and in the original the logo is not on the top of the page (there is already a margin in the PDF). But when I print it, it's located at the exact same position as in the PDF generated by me (and there is no margin in the PDF).

Is there somewhere a print margin already set in the PDF?

cyrodiil
  • 545
  • 1
  • 4
  • 8
  • Aren't you confusing printer margin (which is mechanically set, and not variable in any way you can control by software) with the PDF margin? You'll always get a slight margin when printing something. This is likely some failsafe to keep the printer from inking outside the paper boundries. – bakkerjoeri Jan 11 '13 at 14:46

2 Answers2

130

The following style will effectively set the margins of your document to 0:

@page { margin: 0px; }
body { margin: 0px; }

@page is used by dompdf 0.6.0, body by dompdf 0.5.1. You can modify the margin of the page and body independently, though right now the margin of the two together acts as your content bounds.

BrianS
  • 13,284
  • 15
  • 62
  • 125
28

sometimes you also need to set

html { margin: 0px}
j0k
  • 22,600
  • 28
  • 79
  • 90
Pierlo Upitup
  • 1,614
  • 4
  • 19
  • 27
  • 2
    With dompdf `@page` is, basically, synonymous with `html` in the CSS. You should not need to specify the margins for the `html` element. If your @page rules don't work, check to make sure you have a space between `@page` and the opening brace (as in my answer). There's a bug in some versions of dompdf where the [@page styles are dropped](https://github.com/dompdf/dompdf/issues/614) if that space is missing. – BrianS Apr 05 '13 at 06:00
  • 3 hours of googling, and it was this that fixed everything for me. You are a god among men! – samuraiseoul Mar 24 '17 at 16:23