There are two different things people mean by "turn this web page into a PDF", and most guides blur them:
-
A print PDF. The browser lays the page out for paper, splits it into A4 or Letter pages and keeps the text selectable. This is what
page.pdf()in Puppeteer and Playwright gives you, and what Ctrl+P does. - A visual PDF. The page exactly as it looks on screen, stored as one tall page inside a PDF. No pagination, no print stylesheet, no selectable text.
Pick the wrong one and you spend a day fighting CSS. This guide shows both, with code we ran while writing it, and the numbers from one real page so you can see where each approach goes wrong.
Quick answer
| You need | Use |
|---|---|
| Invoices, reports, documents with selectable or searchable text | Print PDF: Puppeteer or Playwright page.pdf()
|
| A record of what a page looked like (archives, evidence, client sign-off, competitor tracking) | Visual PDF: full-page screenshot saved as PDF |
| Either, for thousands of arbitrary URLs, without running browsers | An API |
What goes wrong: one page, two PDFs
We converted stripe.com/pricing both ways with Puppeteer at a 1280 px viewport.
- Print PDF with defaults (A4): 12 pages. The cookie banner appeared on 11 of the 12 pages, because fixed elements repeat on every printed page. A4 is about 794 CSS pixels wide, so the site switched to its narrower layout, which is not what a desktop visitor sees.
- Print PDF after the fixes below: 8 pages, banner on 1 page, desktop layout kept. Text stayed selectable (about 3,000 words extractable).
- Visual PDF: 1 page, 19,519 px tall, identical to the screen. Zero extractable words, because it is an image.
Neither output is wrong. They answer different questions.
Method 1: Print PDF with Puppeteer (Node.js)
npm install puppeteer
The naive version:
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
await page.pdf({ path: 'page.pdf', format: 'A4', printBackground: true });
await browser.close();
This works on simple pages. On a real marketing site it produces the 12-page result above. Three changes fix most of it:
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.goto('https://stripe.com/pricing', { waitUntil: 'networkidle2' });
// 1. Use the screen stylesheet, not the print one
await page.emulateMediaType('screen');
// 2. Stop fixed and sticky elements (cookie bars, headers, chat widgets) repeating on every page
await page.evaluate(() => {
for (const el of document.querySelectorAll('body *')) {
const pos = getComputedStyle(el).position;
if (pos === 'fixed' || pos === 'sticky') el.style.setProperty('position', 'static', 'important');
}
});
// 3. Make the paper as wide as the viewport so the desktop layout survives
await page.pdf({ path: 'page.pdf', width: '1280px', height: '1800px', printBackground: true });
await browser.close();
Things this still does not handle:
-
Lazy images. Images below the fold may never load. Scroll the page in steps before calling
pdf(). - Cookie banners. Step 2 stops the banner repeating, but one copy still prints over the top of the page. Removing it means a per-site click or selector.
-
Page breaks. Cards and table rows can be cut in half. Add
break-inside: avoidthroughpage.addStyleTag()for the elements you care about. -
Fonts. Web fonts that load late fall back to system fonts. Wait for
document.fonts.ready.
Method 2: Print PDF with Playwright (Python)
Playwright has the same pdf() call, and the same fixes apply:
pip install playwright
playwright install chromium
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page(viewport={"width": 1280, "height": 800})
page.goto("https://stripe.com/pricing", wait_until="networkidle")
page.emulate_media(media="screen")
page.evaluate("""() => {
for (const el of document.querySelectorAll('body *')) {
const pos = getComputedStyle(el).position;
if (pos === 'fixed' || pos === 'sticky') el.style.setProperty('position', 'static', 'important');
}
}""")
page.pdf(path="page.pdf", width="1280px", height="1800px", print_background=True)
browser.close()
On the same page this gave the same 8-page result as the Puppeteer version. pdf() works in Chromium only, not in Firefox or WebKit.
Method 3: Visual PDF (what the page actually looked like)
If the goal is a faithful record, skip the print engine. Take a full-page screenshot and wrap it in a PDF:
import puppeteer from 'puppeteer';
import { PDFDocument } from 'pdf-lib';
import { writeFileSync } from 'fs';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.goto('https://stripe.com/pricing', { waitUntil: 'networkidle2' });
const jpg = await page.screenshot({ type: 'jpeg', quality: 80, fullPage: true });
const doc = await PDFDocument.create();
const img = await doc.embedJpg(jpg);
doc.addPage([img.width, img.height]).drawImage(img, { x: 0, y: 0, width: img.width, height: img.height });
writeFileSync('page.pdf', await doc.save());
await browser.close();
The result matches the screen pixel for pixel, cookie banner included, unless you remove it first. Very tall pages get heavy: our Wikipedia test page was 24,231 px tall and produced a 4.4 MB file.
Method 4: One API call
Running Chromium in production means memory limits, crashed tabs, cookie banners on every other site and a queue for bursts. An API moves that off your servers. SnapRender returns a visual PDF (Method 3) with ads and cookie banners removed by default:
curl "https://app.snap-render.com/v1/screenshot?url=https://stripe.com/pricing&format=pdf&full_page=true&lazy_load=true" \
-H "X-API-Key: YOUR_API_KEY" \
--output page.pdf
Python:
import requests
r = requests.get(
"https://app.snap-render.com/v1/screenshot",
params={"url": "https://stripe.com/pricing", "format": "pdf", "full_page": "true", "lazy_load": "true"},
headers={"X-API-Key": "YOUR_API_KEY"},
timeout=60,
)
r.raise_for_status()
open("page.pdf", "wb").write(r.content)
Node.js:
import fs from 'fs';
const res = await fetch(
'https://app.snap-render.com/v1/screenshot?url=https://stripe.com/pricing&format=pdf&full_page=true&lazy_load=true',
{ headers: { 'X-API-Key': process.env.SNAPRENDER_KEY } }
);
if (!res.ok) throw new Error(await res.text());
await fs.promises.writeFile('page.pdf', Buffer.from(await res.arrayBuffer()));
Be clear about the trade-off: the API's PDF is an image of the page, so the text is not selectable. For invoices and documents, use Method 1 or 2. For archives, evidence, previews and "what did this page say on Tuesday", the visual PDF is the better record. Very tall pages are cut at about 32,000 px.
To see the output before writing code, paste a URL into the free webpage to PDF tool. It runs the same render.
Which one should you use?
| Scenario | Recommended |
|---|---|
| Invoice or report from your own HTML template | Print PDF (Puppeteer or Playwright) |
| Searchable text needed | Print PDF |
| Archive or compliance record of third-party pages | Visual PDF |
| A fresh PDF of the same page every day | API call from your cron job |
| Thousands of user-submitted URLs | API |
| One page, once | The free tool |
A free SnapRender key includes 200 captures a month, PDF included, with no card. Get a key and run the cURL line above.
I build SnapRender, the API in Method 4. Methods 1 to 3 need nothing from us.
Top comments (0)