/pdf API

The pdf API 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 also exposes puppeteer's pdf options via an options property in the JSON body for granular control.

If you want to see all the options check out the schema for it here on GitHub or check out this API schema defined in Swagger.

This route requires either a url or html property in the JSON body. When html is present browserless will render the HTML for you and capture the PDF

Examples

  1. Basic Usage
  2. Custom options
  3. safeMode

Basic Usage

// JSON body
// `options` are the options available via puppeteer's .screenshot method
{
  "url": "https://example.com/",
  "options": {
    "displayHeaderFooter": true,
    "printBackground": false,
    "format": "A0"
  }
}

cURL request

curl -X POST \
  https://chrome.browserless.io/pdf?token=MY_API_TOKEN \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
  "url": "https://example.com/",
  "options": {
    "displayHeaderFooter": true,
    "printBackground": false,
    "format": "A0"
  }
}'

Custom options

The /pdf route has a few special custom options that make it more usable and configurable. We've added these options based on feedback from you in hope that it will help gather best-practices in a single place.

gotoOptions

The gotoOptions is an object that's passed directly into puppeteer's page.goto() call so that you can specify things like alternative loaded events. See puppeteer's goto options for more information.

Example cURL request

curl -X POST \
  https://chrome.browserless.io/pdf?token=MY_API_TOKEN \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
  "url": "https://example.com/",
  "gotoOptions": {
    "waitUntil": "networkidle2",
  },
  "options": {
    "displayHeaderFooter": true,
    "printBackground": false,
    "format": "A0"
  }
}'

safeMode

Because longer pages can crash trigger a "Page Crashed!" error when doing a page.pdf invocation, we've implemented a "safe" way to generate PDF's of bigger websites. When set to true, safeMode will:

  • Capture PDF's one page at a time in-memory.
  • Once all pages are in memory we connect them together.
  • Compress the PDF so it's more efficiently transported over the network.
  • Return the resulting PDF as a binary via application/pdf.

Please note that when using safeMode that it will likely take a while longer to process the request, and the resulting PDF file may be larger.

Example cURL request

curl -X POST \
  https://chrome.browserless.io/pdf?token=MY_API_TOKEN \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
  "url": "https://example.com/",
  "safeMode": true,
  "options": {
    "displayHeaderFooter": true,
    "printBackground": false,
    "format": "A0"
  }
}'

Ready to try benefits of Browserless?