Skip to main content

PDF Generation

The pdf mutation navigates to a URL and generates a PDF. Browserless returns the file as a base64-encoded string with options for page format, margins, headers, footers, and background printing.

Basic Usage

Navigate to a URL and capture the page as a PDF. Wait for elements to load before generating to avoid incomplete content.

mutation PDF {
goto(url: "https://example.com") {
status
}

pdf(displayHeaderFooter: true, printBackground: false, format: a0) {
base64
}
}

Setting HTML Content

The content mutation sets the page HTML directly, without navigating to a URL. Use it to generate PDFs from dynamic or server-rendered markup.

mutation PDF {
content(html: "<h1>Hello, World!</h1>") {
status
}

pdf(displayHeaderFooter: true, printBackground: false, format: a0) {
base64
}
}

Custom header and footer templates let you inject dynamic content into PDFs. Templates support special CSS classes: pageNumber, totalPages, title, url, and date.

Font Size

Specify font sizes explicitly in header and footer templates (e.g., font-size: 12pt). The default text size is too small to read in most PDF viewers.

mutation addFooterAndHeaderToPDF {
goto(url: "https://www.example.com", waitUntil: networkIdle) {
status
}
invoice: pdf(
format: a4
displayHeaderFooter: true
headerTemplate: "<div style='font-size:12pt; line-height:1.2; width:100%; text-align:center; padding-top:2mm;'><span class='title'></span></div>"
footerTemplate: "<div style='font-size:11pt; line-height:1.2; width:100%; text-align:center; padding-bottom:2mm;'><span class='pageNumber'></span>/<span class='totalPages'></span></div>"
printBackground: true
) {
base64
}
}

Next Steps