DocRaptor

No Margin PDFs with DocRaptor

We had a support ticket come in this morning about how to make a PDF with DocRaptor having no margins. What follows is a little background information and the simplest way to achieve the result.

Page Styles in Prince XML and DocRaptor

To understand how to do this, you need to know DocRaptor uses Prince XML to produce PDFs. Prince has several interesting additions to standard DOM/CSS, the most important of which for this tutorial is page styles.

Prince allows us to set styles on a @page element. That element corresponds to an element that wraps everything else that will appear in your PDF. An analog of this you are probably familiar with is the page styles you can set when physically printing documents. I encourage you to poke around the @page-related documentation on Prince’s site. Really cool stuff!

Setting Your Page to Have No Margins

Anyway, back to the tutorial. What we want to do is set our page to have no margins, as Prince’s default works out to around an inch/2.5cm. Here’s some HTML and a picture of the document that it generates.

<html>
  <head>
    <style type="text/css">
      @page { margin: 0; }
      body { font-size: 200%; }
    </style>
  </head>
  <body>
    This content is right up against the edge of the page!
  </body>
</html>

What it looks like:

2011-06-29-no-margin-example

It’s as easy as that. Check out the downloads below to see the actual documents.

Downloads:
* The HTML
* The PDF produced