PDFPipe

Everything else / Custom dimensions

Set a custom PDF page size in millimetres

Any page size at all is reachable by declaring it in the document's own CSS and asking the renderer to honour that page box rather than a named format.

Where the size comes from

The render API accepts six named formats, which covers the paper most documents are printed on and nothing else. Everything with a die, a roll or a holder behind it needs an explicit rectangle, and the CSS page box is where that rectangle belongs: it travels with the document, so the size cannot be lost between the template and the call that renders it.

What gets printed at this size

  • any label, ticket, tag, badge or card stock with a fixed die
  • roll media, where the width is fixed and the length is not
  • regional paper sizes outside the ISO and North American series
  • wristbands, luggage tags and other narrow formats

Margins that survive a real printer

Whatever the stock allows, and the number to find out is the unprintable border, not the sheet size. Die cut label stock usually has none, so zero is correct. Sheet paper always has one, and below about 5mm you are inside it on most desktop printers.

The CSS

Custom dimensions is not one of the six named formats the API accepts, so the size has to come from the document. Declare it in the page box and set prefer_css_page_size in the request.

css
@page {
  /* Width first, then height, in a real print unit. Millimetres and
     inches are safe; pixels are not, because they convert at 96 to the
     inch and any rounding shows up as a hairline. */
  size: 63mm 88mm;
  margin: 3mm;
}

html,
body {
  margin: 0;
  padding: 0;
}

The render request

With a custom size the options carry no format at all: the one flag tells the renderer to use the page box the document declares.

json
{
  "html": "<!doctype html>...",
  "options": {
    // No named format. The page box in the document's own CSS is the
    // size, and this flag is what tells the renderer to use it.
    "prefer_css_page_size": true
  }
}

The mistake people make

Declaring the size in CSS and also passing a named format in the render options. The two disagree, and which one wins is not something you should have to know. Set prefer_css_page_size and leave the format alone.

Frequently asked

How do I set a size the API has no name for?

Declare the rectangle in the document's own CSS page box, in millimetres or inches, and set prefer_css_page_size on the request. Do not also pass a named format: the two disagree and you should not have to know which wins.

Can I pass Custom dimensions as a format?

No. The API accepts six named formats: A4, A3, A5, Letter, Legal and Tabloid. Custom dimensions is reached through the document's page box with prefer_css_page_size set, which is a supported path and not a workaround.

Does landscape need a different page size?

No. Landscape is the same rectangle turned, so it is set as an orientation rather than as swapped dimensions. Writing the width and height the other way round produces the same page and makes the intent harder to read later.

Other page sizes

Sizes that come up in the same conversation, and one from each of the other families.

Paste the CSS below into the playground and see the page box it produces.