Skip to main content

/pdf API

The /pdf API allows for simple navigation to a site and capturing a PDF. Browserless returns with 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.

You can view the complete PDF API OpenAPI specification for all options and properties.

info
  • This route requires either a url or html property in the JSON body. Browserless will render the Webpage/HTML for you and capture the PDF.
  • Browserless does't allow setting a options.path for /pdf API.

Example

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

Configuration Options

Browserless provides you an extensive set of options for you customize this API to your unique needs, we have discussed some of the configurations below:

Setting HTML content

You can set the HTML content of the page to render dynamically generated content as well. Checkout the below example containing the request in the specified format, with examples for curl, JavaScript, and Python.

danger

When this propriety is set, the url propriety must not be present.

curl -X POST \
"https://production-sfo.browserless.io/pdf?token=YOUR_API_TOKEN_HERE" \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"html": "<h1>Hello World!</h1>",
"options": {
"displayHeaderFooter": true,
"printBackground": false,
"format": "A0"
}
}'

Adding custom styles and scripts

Browserless offers ways to load additional stylesheets and script tags to the page as well. This give you full control and allows you to override page elements to suite your needs.

curl -X POST \
"https://production-sfo.browserless.io/pdf?token=YOUR_API_TOKEN_HERE" \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"url": "https://example.com/",
"addScriptTag": [
{ "url": "https://code.jquery.com/jquery-3.7.1.min.js" },
{ "content": "document.querySelector(`h1`).innerText = `Hello World!`" }
],
"addStyleTag": [
{
"content": "body { height: 100vh; background: linear-gradient(45deg, #da5a44, #a32784); }"
},
{
"url": "https://interactive-examples.mdn.mozilla.net/live-examples/css-examples/text-decoration/text-decoration-color.css"
}
]
}'

Additional Configuration Options

The /pdf API supports additional configuration options that are shared across multiple REST API endpoints. See the Request Configuration page for detailed documentation on:

  • Waiting for Things: Wait for events, functions, selectors, or timeouts before generating the PDF
  • Navigation Options: Customize navigation behavior using gotoOptions
  • Rejecting Undesired Requests: Block specific resources using rejectResourceTypes and rejectRequestPattern
  • Continue on Error: Use bestAttempt to proceed when async events fail or timeout

Fullpage PDF

The /pdf REST API doesn't support creating a single long-page PDF file that captures an entire webpage on one page. However, you can create custom full-page PDFs using our /function API.

The /function API gives you full control over the PDF generation process, allowing you to calculate the page height dynamically and format your PDF accordingly. This is particularly useful when you need to capture an entire webpage as a single continuous page rather than breaking it into multiple pages.

For a complete example of generating full-page PDFs, including code snippets in multiple languages, see the Advanced Usage section in the /function API documentation.

PDF Metadata

Browserless's /pdf API uses Puppeteer under the hood to generate PDFs. By default, Puppeteer doesn't provide a built-in API to set PDF metadata (like Title, Author, Subject, Keywords, etc.) when generating a PDF with page.pdf().

Puppeteer relies on Chrome's printToPDF DevTools protocol, which only exposes a limited set of options (like page size, margins, header/footer, etc.), but not document metadata.

The workaround is to generate the PDF with Browserless first, then adjust the metadata with a library such as pdf-lib.