PDF4.dev
Learn

PDF format

Control the page size, margins, and global styles of your PDFs with the format object.

Presets

6 built-in presets are available:

PresetDimensionsUse case
a4210 x 297mmDefault. Standard in most countries.
a4-landscape297 x 210mmWide reports, dashboards
letter216 x 279mmStandard in the US
letter-landscape279 x 216mmUS landscape documents
square210 x 210mmSocial media, certificates
customUser-definedAny size with width and height

Default: a4 portrait with 20mm top/bottom and 15mm left/right margins.

Margins

Margins (and width, height) take a length in px, pt, pc, mm, cm or in, or a bare number of px (e.g. 20mm, 1in, 72pt, 10). A missing side takes its default and an empty one is 0. component_gap is CSS, so em, rem and % work there too. Presets and alignments are case-insensitive (A4 is a4).

A template write refuses an unknown preset, alignment or footer position, or a length it cannot read (em for a margin), with a 400 invalid_pdf_format naming the field. A value sent back exactly as it is stored is kept, so a template saved before these checks still saves. A render is more forgiving, so integrations keep printing what they printed: an unknown preset prints A4, an unknown alignment or footer position is ignored, and only a margin (or a custom width or height) it cannot read answers 400 invalid_format.

{
  "format": {
    "preset": "a4",
    "margins": {
      "top": "20mm",
      "bottom": "20mm",
      "left": "15mm",
      "right": "15mm"
    }
  }
}

Custom dimensions

Set preset to custom and provide width and height:

{
  "format": {
    "preset": "custom",
    "width": "100mm",
    "height": "150mm",
    "margins": {
      "top": "5mm",
      "bottom": "5mm",
      "left": "5mm",
      "right": "5mm"
    }
  }
}

Global styles

Optional fields that style the whole page. The Google Fonts stylesheet goes in the <head>; the other rules are added as a <style> at the end of the <body>, marked !important:

FieldDescriptionExample
background_colorPage background color"#ffffff"
font_familyDefault font family"Inter, sans-serif"
font_sizeDefault font size"14px"
colorText color"#333333"
line_heightLine height"1.5" or "24px"
google_fonts_urlGoogle Fonts URL (or any @import-compatible CSS URL). Loaded in <head>, available to the template and all attached components."https://fonts.googleapis.com/css2?family=Roboto&display=swap"
component_gapGap between header/footer components and page content"5mm"
footer_positionFooter position mode: "after-content" (default) places footer inline after content, "page-bottom" pins footer to the bottom of every page"after-content"
text_alignText alignment on body ("left", "center", "right", "justify")"left"
horizontal_alignHorizontal content alignment ("left", "center", "right")"center"
vertical_alignVertical content alignment within the page ("top", "center", "bottom"). With a header or footer, the content is aligned in the space between them."center"
Full format example
{
  "format": {
    "preset": "a4",
    "margins": {
      "top": "25mm",
      "bottom": "25mm",
      "left": "20mm",
      "right": "20mm"
    },
    "background_color": "#ffffff",
    "font_family": "Roboto, sans-serif",
    "font_size": "14px",
    "color": "#333333",
    "line_height": "1.5",
    "google_fonts_url": "https://fonts.googleapis.com/css2?family=Roboto&display=swap",
    "component_gap": "5mm",
    "footer_position": "after-content",
    "text_align": "left",
    "horizontal_align": "center",
    "vertical_align": "top"
  }
}

These rules are set on the <body> with !important, so they win over the template's own body rules. Elements that set their own font, size or color (inline or in a <style> rule that targets them) keep it: the format only changes what they inherit.

Overriding at render time

Templates have a stored pdf_format. You can override it per render request by passing format in the request body:

Override margins for a specific render
{
  "template_id": "invoice",
  "data": { "company": "Acme" },
  "format": {
    "preset": "letter",
    "margins": {
      "top": "10mm",
      "bottom": "10mm",
      "left": "10mm",
      "right": "10mm"
    }
  }
}

The format in the request replaces the template's stored format as a whole: it is not merged, and the fields it leaves out take the defaults (here, the template's background or font would not apply). A margins object prints the sides it leaves out at 0, as the render always has: send all four sides, or leave margins out for the default margins. A margin that cannot be read answers 400 invalid_format, with a message naming it.

Component rendering

Header and footer components are rendered using <thead> and <tfoot> elements inside a full-page table. Chromium's print engine automatically repeats <thead> at the top and <tfoot> at the bottom of every printed page, making this approach reliable for multi-page documents.

Chromium repeats a header or footer only while it fits in a quarter of the page content height (the page minus its margins), component_gap included. A taller one prints on the first page only.

The component_gap field controls the spacing between the header/footer and the page content. It is applied as padding on the <thead> and <tfoot> cells:

{
  "format": {
    "preset": "a4",
    "margins": { "top": "20mm", "bottom": "20mm", "left": "15mm", "right": "15mm" },
    "component_gap": "8mm"
  }
}

Components inherit all global styles from the format (font family, font size, color, line height, etc.) since they are injected into the same document body.

The footer_position field controls where the footer renders on the page:

ValueBehavior
"after-content" (default)Footer sits right after the last content row. On multi-page documents, it repeats at the bottom of intermediate pages but follows content on the last page.
"page-bottom"Footer is pinned to the bottom of every page, including the last page. Uses CSS position:fixed so it always appears at the page bottom regardless of content length.
{
  "format": {
    "preset": "a4",
    "margins": { "top": "20mm", "bottom": "20mm", "left": "15mm", "right": "15mm" },
    "component_gap": "5mm",
    "footer_position": "page-bottom"
  }
}