Custom page layouts in PDF

Available in Start planBetav0.3.0

The PDF Export extension supports custom page sizes and margins, allowing you to create documents with precise layouts that match your requirements. Whether you need A5 pages, custom paper sizes, or specific margin configurations, you can configure these settings directly in the extension.

Page size configuration

Use the pageSize option to define custom page dimensions for your exported PDF files. The page size configuration accepts width and height values with various measurement units.

Supported units

All page size and margin measurements support the following units:

UnitDescription
cmCentimeters (default)
inInches
ptPoints
pcPicas
mmMillimeters
pxPixels

Configuration options

PropertyTypeDescriptionDefault
widthstringThe width of the page. Must be a positive number followed by a valid unit (cm, in, pt, pc, mm, px)."21cm"
heightstringThe height of the page. Must be a positive number followed by a valid unit (cm, in, pt, pc, mm, px)."29.7cm"
import { ExportPdf } from '@tiptap-pro/extension-export-pdf'

ExportPdf.configure({
  token: 'YOUR_TOKEN',
  appId: 'YOUR_APP_ID',
  pageSize: {
    width: '14.8cm',    // A5 width
    height: '21cm',     // A5 height
  },
})

Page margins configuration

The pageMargins option allows you to set custom margins for all sides of your document pages. Unlike page sizes, top and bottom margins can accept negative values.

Configuration options

PropertyTypeDescriptionDefault
topstringThe top margin of the page. Can be negative. Must be a number followed by a valid unit (cm, in, pt, pc, mm, px)."1cm"
bottomstringThe bottom margin of the page. Can be negative. Must be a number followed by a valid unit (cm, in, pt, pc, mm, px)."1cm"
leftstringThe left margin of the page. Must be a positive number followed by a valid unit (cm, in, pt, pc, mm, px)."1cm"
rightstringThe right margin of the page. Must be a positive number followed by a valid unit (cm, in, pt, pc, mm, px)."1cm"
import { ExportPdf } from '@tiptap-pro/extension-export-pdf'

ExportPdf.configure({
  token: 'YOUR_TOKEN',
  appId: 'YOUR_APP_ID',
  pageMargins: {
    top: '2cm',
    bottom: '2cm',
    left: '1.5cm',
    right: '1.5cm',
  },
})

Complete example

Here's a complete example showing how to configure custom page layouts with the PDF Export extension:

import { ExportPdf } from '@tiptap-pro/extension-export-pdf'

const editor = new Editor({
  extensions: [
    // Other extensions...
    ExportPdf.configure({
      token: 'YOUR_TOKEN',
      appId: 'YOUR_APP_ID',
      // A5 page size
      pageSize: {
        width: '14.8cm',
        height: '21cm',
      },
      // Custom margins
      pageMargins: {
        top: '2cm',
        bottom: '2cm',
        left: '2cm',
        right: '2cm',
      },
    }),
    // Other extensions...
  ],
})

// Export with custom layout
editor
  .chain()
  .exportPdf({
    onCompleteExport(result) {
      const url = URL.createObjectURL(result)
      const a = document.createElement('a')

      a.href = url
      a.download = 'custom-layout.pdf'
      a.click()

      URL.revokeObjectURL(url)
    },
  })
  .run()

Common page sizes

Here are some common page sizes you can use as reference:

FormatWidthHeight
A421cm29.7cm
A514.8cm21cm
Letter8.5in11in
Legal8.5in14in
Tabloid11in17in

Different measurement units

You can mix and match different units based on your preference:

ExportPdf.configure({
  token: 'YOUR_TOKEN',
  appId: 'YOUR_APP_ID',
  // US Letter size in inches
  pageSize: {
    width: '8.5in',
    height: '11in',
  },
  // Margins in different units
  pageMargins: {
    top: '0.75in',    // Inches
    bottom: '72pt',   // Points (1 inch = 72 points)
    left: '2cm',      // Centimeters
    right: '20mm',    // Millimeters
  },
})

Unit consistency

While you can mix different units, it's generally recommended to use consistent units throughout your configuration for better maintainability and clarity.