Skip to main content

PDFs

The pdf mutation allows for simple navigation to a site and capturing a PDF. Browserless will respond with a Content-Type of application/pdf and a Buffer of the PDF file. Similar to screenshots, this REST API also exposes puppeteer's pdf options via an options property in the JSON body for granular control.

On this collection we'll look at:

BQL Schemas

For more details on BQL mutations, refer to the BrowserQL Schema reference pages.

Rest API

Generating PDFs can also be done with Browserless Rest API. For endpoint details, parameters, and code samples, see the Browserless REST API.

Basic Usage

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

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

For more reliable PDF generation, it's important to wait for the site and elements to be ready before capturing. Learn more about BrowserQL's built-in wait methods in our Waiting for Things documentation.

Setting HTML Content

You can set the HTML content of the page to render dynamically generated content as well.

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

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

You can add custom header and footer templates to your PDFs with dynamic content like page numbers. The templates support special CSS classes for injecting values: pageNumber, totalPages, title, url, and date.

Font Size Recommendation

When using header and footer templates, you may need to specify font sizes explicitly (e.g., font-size: 16pt) as the default text may appear too small depending on your page format.

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

Ready to take your PDF generation to the next level? Explore these key areas: