Generate a PDF
Export any webpage or HTML content as a PDF using the Browserless PDF API.
- A Browserless API token from your account dashboard
Steps
- REST API
- Frameworks
- BQL
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://example.com",
"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://example.com',
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://example.com',
'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://example.com",
"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://example.com",
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://example.com', { 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://example.com', { 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://example.com") {
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