Skip to main content
With Browserbase, you can work with PDFs in a few ways:
  1. Generate: Create PDFs from web pages with Playwright’s page.pdf() method.
  2. Download: Auto-download PDFs to Browserbase’s cloud storage by opening a PDF URL. To retrieve them, see our Downloads documentation.
  3. View: Display PDFs in the browser, instead of downloading them, by setting the enablePdfViewer property in your browser settings.

Generate PDFs

After creating and connecting to a session, here’s how to generate a PDF from a web page using Playwright:
Playwright
import { chromium } from "playwright-core";
import { Browserbase } from "@browserbasehq/sdk";

(async () => {
  console.log("Starting remote browser...");
  const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
  const session = await bb.sessions.create({
    projectId: process.env.BROWSERBASE_PROJECT_ID,
  });

  const browser = await chromium.connectOverCDP(session.connectUrl);
  const defaultContext = browser.contexts()[0];
  const page = defaultContext.pages()[0];

  await page.goto("https://news.ycombinator.com");

  console.log("Generating PDF...");

  await page.pdf({
    path: "webpage.pdf",
    format: "A4",
  });

  console.log("Shutting down...");
  await page.close();
  await browser.close();
})().catch((error) => {
  console.error(error);
});

Download PDFs

When you navigate to a PDF URL, Browserbase automatically downloads the PDF file and cancels the navigation. The downloaded PDF is stored in Browserbase’s cloud storage for later retrieval.
import { chromium } from "playwright-core";
import { Browserbase } from "@browserbasehq/sdk";

(async () => {
  console.log("Starting remote browser...");
  const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
  const session = await bb.sessions.create({
    projectId: process.env.BROWSERBASE_PROJECT_ID,
  });

  const browser = await chromium.connectOverCDP(session.connectUrl);
  const defaultContext = browser.contexts()[0];
  const page = defaultContext.pages()[0];

  // Configure download behavior
  const client = await defaultContext.newCDPSession(page);
  await client.send("Browser.setDownloadBehavior", {
    behavior: "allow",
    downloadPath: "downloads",
    eventsEnabled: true,
  });

  // Navigate to PDF and trigger download
  console.log("Attempting to download PDF...");
  const [download] = await Promise.all([
    page.waitForEvent("download"),
    page.goto("https://constitutioncenter.org/media/files/constitution.pdf").catch(() => {
      console.log("Navigation cancelled due to download (expected behavior)");
    })
  ]);

  let downloadError = await download.failure();
  if (downloadError !== null) {
    console.log("Error happened on download:", downloadError);
    throw new Error(downloadError);
  }

  console.log("PDF download completed successfully");

  console.log("Shutting down...");
  await page.close();
  await browser.close();
})().catch((error) => {
  console.error(error);
});
After the PDF is downloaded, you can retrieve it from Browserbase’s cloud storage. See the Downloads documentation for information on how to access your downloaded files.
View or run the example template here

View PDFs

To view a PDF in the browser tab instead of automatically downloading it, you can set the enablePdfViewer property to true as follows:
const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
const session = await bb.sessions.create({
  projectId: process.env.BROWSERBASE_PROJECT_ID,
  browserSettings: {
    enablePdfViewer: true,
  }
});