Skip to content

Getting Started with PDF

The PDF backend lets you script PDF documents using pdf-lib. Your main() function returns a PDFDocument (or a raw Uint8Array), which is displayed instantly in the preview pane and downloadable as a .pdf file.

Open the Modelling tab and click New project, choosing PDF as the backend. A starter file is created for you. Hit ▶ Run to see it rendered.

const { PDFDocument, StandardFonts, rgb } = require('pdf-lib')
async function main() {
const doc = await PDFDocument.create()
const font = await doc.embedFont(StandardFonts.Helvetica)
const page = doc.addPage([595, 842]) // A4 in points
page.drawText('Hello, pdf-lib!', {
x: 50, y: 780,
size: 24,
font,
color: rgb(0.1, 0.1, 0.4),
})
return doc
}

main() must be async because pdf-lib’s API is Promise-based. PDFDocument.create() and doc.embedFont() both return Promises.

pdf-lib uses the PDF coordinate system: origin at the bottom-left, y increasing upward. For A4 (595 × 842 pt) the top of the page is y = 842.

A4 position x y
Top-left 50 ~792
Centre ~297 ~421
Bottom-left 50 ~50

Points (pt) are the native PDF unit — 1 pt = 1/72 inch.

Use page.drawLine(), page.drawRectangle(), and page.drawEllipse() to add vector graphics:

// Horizontal rule
page.drawLine({
start: { x: 50, y: 760 },
end: { x: 545, y: 760 },
thickness: 1,
color: rgb(0.7, 0.7, 0.7),
})
// Filled rectangle
page.drawRectangle({
x: 50, y: 600,
width: 495, height: 140,
color: rgb(0.95, 0.97, 1),
borderColor: rgb(0.2, 0.4, 0.8),
borderWidth: 1.5,
})
// Circle (ellipse with equal radii)
page.drawEllipse({
x: 297, y: 500,
xScale: 40, yScale: 40,
color: rgb(0.2, 0.6, 0.3),
})

Embed one of the 14 standard PDF fonts (always available, no extra data) or load a custom TTF/OTF font from the workspace.

const { StandardFonts } = require('pdf-lib')
const fontReg = await doc.embedFont(StandardFonts.Helvetica)
const fontBold = await doc.embedFont(StandardFonts.HelveticaBold)
const fontMono = await doc.embedFont(StandardFonts.Courier)

Standard fonts: Helvetica, HelveticaBold, HelveticaOblique, HelveticaBoldOblique, Courier, CourierBold, Times-Roman, Symbol, ZapfDingbats, and their variants.

pdf-lib’s drawText places a single string — wrap text manually with an array of lines and a running y counter:

const body = [
'This document was generated by a script.',
'Each line is drawn individually.',
'',
'Blank lines are skipped to create spacing.',
]
let y = 740
for (const line of body) {
if (line) page.drawText(line, { x: 50, y, size: 13, font })
y -= 20
}

Call doc.addPage() for each page. Pages share embedded fonts because they belong to the same document:

for (let i = 0; i < pageCount; i++) {
const page = doc.addPage([595, 842])
page.drawText(`Page ${i + 1} of ${pageCount}`, {
x: 50, y: 800, size: 12, font,
})
}

Export a getParameterDefinitions() function to expose live controls. Parameters re-run main() automatically when changed:

function getParameterDefinitions() {
return [
{ name: 'title', type: 'text', default: 'My Report', caption: 'Title' },
{ name: 'pageCount', type: 'number', widget: 'slider', default: 3, min: 1, max: 20, caption: 'Pages' },
{ name: 'darkMode', type: 'boolean', default: false, caption: 'Dark background' },
]
}
async function main({ title, pageCount, darkMode }) {
const bg = darkMode ? rgb(0.1, 0.1, 0.1) : rgb(1, 1, 1)
// …
}

Once the preview looks right, click ↓ Download in the viewer toolbar to save the .pdf file.

  • Open Edit imports… to browse all pdf-lib exports organised by category.
  • Try the Forms and fields tutorial to create interactive PDF forms.