Generate a PDF
Export any webpage or HTML content as a PDF using Browserless.
- A Browserless API token from your account dashboard
Steps
- AI Agent
- REST API
- Frameworks
- BQL
Use the Browserless MCP server to generate a PDF from any MCP-compatible AI agent (Claude Desktop, Cursor, Windsurf, ChatGPT, etc.).
1. Connect the MCP server
Send this prompt to your AI agent to install the Browserless MCP server:
Go to https://github.com/browserless/browserless-mcp/blob/main/install.md
and follow the instructions to install the Browserless MCP server
for my client.
2. Generate a PDF
Use browserless_smartscraper. It captures the page as a PDF in one call with automatic bot-protection handling.
Use the browserless_smartscraper tool to generate a full-page
PDF of https://scraping-sandbox.netlify.app/dashboard-report and save it as output.pdf
Use the /pdf REST endpoint to generate a PDF from a URL. No WebSocket connection needed.
- cURL
- JavaScript
- Python
- Java
- C#
1. Build the request
Append your token to the PDF endpoint:
https://production-sfo.browserless.io/pdf?token=YOUR_API_TOKEN_HERE
2. Send the request
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://scraping-sandbox.netlify.app/dashboard-report",
"options": {
"displayHeaderFooter": true,
"printBackground": true,
"format": "A4"
}
}' \
-o output.pdf
3. Check the output
The response is a raw PDF binary written to output.pdf in the current directory.
1. Send the request
import fs from 'fs';
const response = await fetch(
'https://production-sfo.browserless.io/pdf?token=YOUR_API_TOKEN_HERE',
{
method: 'POST',
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://scraping-sandbox.netlify.app/dashboard-report',
options: {
displayHeaderFooter: true,
printBackground: true,
format: 'A4',
},
}),
}
);
const buffer = await response.arrayBuffer();
fs.writeFileSync('output.pdf', Buffer.from(buffer));
2. Check the output
Run the script with node pdf.mjs. The file output.pdf will appear in the current directory.
1. Install dependencies
pip install requests
2. Send the request
import requests
response = requests.post(
'https://production-sfo.browserless.io/pdf?token=YOUR_API_TOKEN_HERE',
headers={'Cache-Control': 'no-cache', 'Content-Type': 'application/json'},
json={
'url': 'https://scraping-sandbox.netlify.app/dashboard-report',
'options': {
'displayHeaderFooter': True,
'printBackground': True,
'format': 'A4',
},
},
)
with open('output.pdf', 'wb') as f:
f.write(response.content)
3. Check the output
Run the script with python pdf.py. The file output.pdf will appear in the current directory.
1. Send the request
import java.io.*;
import java.net.URI;
import java.net.http.*;
String token = "YOUR_API_TOKEN_HERE";
String endpoint = "https://production-sfo.browserless.io/pdf?token=" + token;
String jsonData = """
{
"url": "https://scraping-sandbox.netlify.app/dashboard-report",
"options": {
"displayHeaderFooter": true,
"printBackground": true,
"format": "A4"
}
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpoint))
.header("Cache-Control", "no-cache")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonData))
.build();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (FileOutputStream fos = new FileOutputStream("output.pdf")) {
response.body().transferTo(fos);
}
System.out.println("PDF saved as output.pdf");
2. Check the output
Run the class. The file output.pdf will appear in the current directory.
1. Send the request
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
string token = "YOUR_API_TOKEN_HERE";
string endpoint = $"https://production-sfo.browserless.io/pdf?token={token}";
var payload = new
{
url = "https://scraping-sandbox.netlify.app/dashboard-report",
options = new { displayHeaderFooter = true, printBackground = true, format = "A4" },
};
using (HttpClient httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
request.Headers.Add("Cache-Control", "no-cache");
request.Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
var contentStream = await response.Content.ReadAsStreamAsync();
using (var fileStream = new FileStream("output.pdf", FileMode.Create, FileAccess.Write))
{
await contentStream.CopyToAsync(fileStream);
}
Console.WriteLine("PDF saved as output.pdf");
}
2. Check the output
Run the program. The file output.pdf will appear in the current directory.
Use a browser connection when you need to interact with the page or wait for dynamic content before generating the PDF.
- Puppeteer
- Playwright
1. Install dependencies
npm install puppeteer-core
2. Connect and generate
import puppeteer from 'puppeteer-core';
import fs from 'fs';
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://production-sfo.browserless.io?token=YOUR_API_TOKEN_HERE',
});
try {
const page = await browser.newPage();
await page.goto('https://scraping-sandbox.netlify.app/dashboard-report', { waitUntil: 'networkidle2' });
const pdf = await page.pdf({
format: 'A4',
printBackground: true,
displayHeaderFooter: true,
});
fs.writeFileSync('output.pdf', pdf);
} finally {
// Always close to release the session even on error.
await browser.close();
}
3. Check the output
Run the script with node pdf.mjs. The file output.pdf will appear in the current directory.
1. Install dependencies
npm install playwright-core
2. Connect and generate
import { chromium } from 'playwright-core';
import fs from 'fs';
const browser = await chromium.connect(
'wss://production-sfo.browserless.io/chromium/playwright?token=YOUR_API_TOKEN_HERE'
);
try {
const page = await browser.newPage();
await page.goto('https://scraping-sandbox.netlify.app/dashboard-report', { waitUntil: 'networkidle' });
const pdf = await page.pdf({
format: 'A4',
printBackground: true,
displayHeaderFooter: true,
});
fs.writeFileSync('output.pdf', pdf);
} finally {
// Always close to release the session even on error.
await browser.close();
}
3. Check the output
Run the script with node pdf.mjs. The file output.pdf will appear in the current directory.
1. Write the mutation
Navigate to the URL and generate a PDF in one operation:
mutation PDF {
goto(url: "https://scraping-sandbox.netlify.app/dashboard-report") {
status
}
pdf(format: a4, printBackground: true, displayHeaderFooter: true) {
base64
}
}
2. Run it
Paste into the BQL IDE and click Run.
3. Check the output
The response includes a base64-encoded PDF. Each example above decodes it and saves it as output.pdf.
{
"data": {
"goto": { "status": 200 },
"pdf": { "base64": "JVBERi0xLjQ..." }
}
}
Next steps
- Take a Screenshot — capture the page as an image instead
- Scrape Structured Data — extract data from the page
- Fill and Submit a Form — automate form interactions before generating a PDF