HTML to PDF in Node.js, and “Could not find Chrome” in production.
To convert HTML to PDF in Node.js, load the HTML in headless Chrome with Puppeteer or Playwright and call page.pdf(). In production, the usual failure is Could not find Chrome (ver. …): the browser Puppeteer downloaded at install time is not on the machine running your code. Install it there, or point puppeteer-core at a Chrome that is.
Facts checked
“Could not find Chrome”: the exact error
puppeteer.launch() throws this when the Chrome build that your Puppeteer version expects is not in its cache folder. The version and the path change with your setup:
Error: Could not find Chrome (ver. 158.0.8127.88). This can occur if either
1. you did not perform an installation before running the script (e.g. `npx puppeteer browsers install chrome`) or
2. your cache path is incorrectly configured (which is: /home/app/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.What the message means
The puppeteer package downloads Chrome in its postinstall script and keeps it in ~/.cache/puppeteer, in the home folder of the user who ran the install, not in node_modules. Anything that separates the install from the machine that runs the code loses the browser: a build container that is thrown away, a Docker stage you don’t copy from, a deploy that ships only the project folder, or an install with scripts turned off.
The fix is one of three. Install Chrome where the code runs (npx puppeteer browsers install chrome in that image). Keep the cache inside the project with a .puppeteerrc.cjs so it ships with the code. Or use puppeteer-core, which never downloads a browser, and give it executablePath to a Chrome or Chromium that is installed on the server.
// .puppeteerrc.cjs, at the project root: keep the browser inside the project,
// so it ships with the code instead of staying in the build machine's home folder
const { join } = require("path");
/** @type {import("puppeteer").Configuration} */
module.exports = {
cacheDirectory: join(__dirname, ".cache", "puppeteer"),
};
// Then, in the build step that runs on the same image as production:
// npx puppeteer browsers install chromeThe ways to make a PDF from Node.js
| Option | What it renders | What the server needs | Status |
|---|---|---|---|
Puppeteer (puppeteer or puppeteer-core) † | Real Chrome: modern CSS, web fonts, JavaScript. page.pdf() has paper format, margins, header and footer templates, and printBackground (off by default) | A Chrome or Chromium binary and its system libraries | Maintained |
Playwright (playwright or playwright-core) † | The same Chromium engine. page.pdf() works only with Chromium, not Firefox or WebKit | A Chromium binary and its system libraries | Maintained |
| pdfkit, pdf-lib † † | No HTML. pdfkit draws text, shapes and images that you place; pdf-lib creates and modifies PDFs (merge, fill forms, stamp) | Nothing native | Maintained |
| wkhtmltopdf and its npm wrappers † † | Qt WebKit, an engine Qt deprecated in 2015 and removed in 2016 | A native binary | GitHub repository archived; last release 0.12.6 (June 2020). Its own site says not to use it with untrusted HTML |
| html-pdf (npm) † | PhantomJS | The PhantomJS binary | Deprecated on npm: “Please migrate your projects to a newer library like puppeteer”. Last release 3.0.1 (May 2021) |
| Canner document rendering | Headless Chromium, HTML that carries its own CSS, images and fonts. Scripts run | Nothing: one socket, on Live and above | Presets only, 50 MB in, 2 minutes a job |
† Competitor details come from each vendor’s own published pages on the dates shown; prices and plans change, so check the vendor before you decide. Puppeteer (`puppeteer` or `puppeteer-core`) (2026-11-25) · Playwright (`playwright` or `playwright-core`) (2026-11-25) · pdfkit, pdf-lib (2026-11-25) · pdfkit, pdf-lib (2026-11-25) · wkhtmltopdf and its npm wrappers (2026-11-25) · wkhtmltopdf and its npm wrappers (2026-11-25) · html-pdf (npm) (2026-11-25)
Puppeteer: a production setup
Launching Chrome takes time and memory, so start one browser per process and open a page per PDF. Close the page in finally, or every failed render leaves a tab open. --disable-dev-shm-usage makes Chrome keep its shared memory files in /tmp instead of /dev/shm, which is small in many containers.
With puppeteer-core you choose the binary. With puppeteer you can drop executablePath and it uses the one it downloaded. In Puppeteer 25 the TypeScript types for setContent no longer offer networkidle0; wait with page.waitForNetworkIdle() after it, as here.
// pdf.mjs: one browser for the whole process, one page per PDF
import puppeteer from "puppeteer-core";
let browserPromise;
function getBrowser() {
browserPromise ??= puppeteer.launch({
executablePath: process.env.CHROME_PATH, // e.g. /usr/bin/chromium
args: ["--disable-dev-shm-usage"],
});
return browserPromise;
}
export async function htmlToPdf(html) {
const browser = await getBrowser();
const page = await browser.newPage();
try {
await page.setContent(html, { waitUntil: "load" });
await page.waitForNetworkIdle({ idleTime: 500 }); // images, fonts, fetches
return await page.pdf({
format: "A4",
printBackground: true,
margin: { top: "15mm", bottom: "15mm", left: "12mm", right: "12mm" },
});
} finally {
await page.close(); // always: a leaked page keeps its memory
}
}
// const pdf = await htmlToPdf("<h1>Invoice 1042</h1>");Playwright: the same thing
Playwright’s page.pdf() renders with print CSS already; emulateMedia is there so you can switch to screen if your layout only has screen styles. preferCSSPageSize: true lets an @page { size: … } rule in the HTML set the paper.
import { chromium } from "playwright-core"; // or "playwright"
const browser = await chromium.launch({
executablePath: process.env.CHROME_PATH, // omit with the full "playwright" package
});
export async function htmlToPdf(html) {
const page = await browser.newPage();
try {
await page.setContent(html, { waitUntil: "load" });
await page.emulateMedia({ media: "print" });
return await page.pdf({ format: "Letter", printBackground: true, preferCSSPageSize: true });
} finally {
await page.close();
}
}Why Chrome is missing in production, and the fix for each
| Cause | What you see | Fix |
|---|---|---|
The cache stayed on the build machine: ~/.cache/puppeteer is outside the project, and the runtime is a different container, stage or user † † | Could not find Chrome (ver. …) with a cache path that doesn’t exist at runtime | Set cacheDirectory in .puppeteerrc.cjs (or PUPPETEER_CACHE_DIR) to a folder inside the project, or run npx puppeteer browsers install chrome in the runtime image |
The download never ran: PUPPETEER_SKIP_DOWNLOAD, --ignore-scripts, or pnpm, which blocks dependency build scripts not listed in allowBuilds † † | The same error, on every deploy | Allow the script (allowBuilds in pnpm), run the install command yourself in the build, or use puppeteer-core with an installed Chrome |
Serverless size limits: the puppeteer package’s Chrome for Linux is about 280 MB, over Vercel’s 250 MB uncompressed function limit and AWS Lambda’s 250 MB unzipped package † † † | The build fails (… exceeds the maximum uncompressed size limit of 250mb), or the function deploys and then throws Could not find Chrome, because the cache folder isn’t in the bundle | puppeteer-core plus @sparticuz/chromium (or -min, with the binary hosted elsewhere). Details below |
Missing shared libraries on a slim image (node:22-slim, Cloud Run) † † † | Failed to launch the browser process! followed by error while loading shared libraries: libnss3.so | Install Chrome’s dependencies: npx puppeteer browsers install chrome --install-deps on Debian or Ubuntu, or the distribution’s chromium package, or the ghcr.io/puppeteer/puppeteer image |
| Running as root in a container without a usable sandbox † † | Running as root without --no-sandbox is not supported, or No usable sandbox! | Run as a non-root user. --no-sandbox works, but Puppeteer strongly discourages it unless you trust every page you open |
† Competitor details come from each vendor’s own published pages on the dates shown; prices and plans change, so check the vendor before you decide. The cache stayed on the build machine: `~/.cache/puppeteer` is outside the project, and the runtime is a different container, stage or user (2026-11-25) · The cache stayed on the build machine: `~/.cache/puppeteer` is outside the project, and the runtime is a different container, stage or user (2026-11-25) · The download never ran: `PUPPETEER_SKIP_DOWNLOAD`, `--ignore-scripts`, or pnpm, which blocks dependency build scripts not listed in `allowBuilds` (2026-11-25) · The download never ran: `PUPPETEER_SKIP_DOWNLOAD`, `--ignore-scripts`, or pnpm, which blocks dependency build scripts not listed in `allowBuilds` (2026-11-25) · Serverless size limits: the `puppeteer` package’s Chrome for Linux is about 280 MB, over Vercel’s 250 MB uncompressed function limit and AWS Lambda’s 250 MB unzipped package (2026-11-25) · Serverless size limits: the `puppeteer` package’s Chrome for Linux is about 280 MB, over Vercel’s 250 MB uncompressed function limit and AWS Lambda’s 250 MB unzipped package (2026-11-25) · Serverless size limits: the `puppeteer` package’s Chrome for Linux is about 280 MB, over Vercel’s 250 MB uncompressed function limit and AWS Lambda’s 250 MB unzipped package (2026-11-25) · Missing shared libraries on a slim image (`node:22-slim`, Cloud Run) (2026-11-25) · Missing shared libraries on a slim image (`node:22-slim`, Cloud Run) (2026-11-25) · Missing shared libraries on a slim image (`node:22-slim`, Cloud Run) (2026-11-25) · Running as root in a container without a usable sandbox (2026-11-25) · Running as root in a container without a usable sandbox (2026-11-25)
Puppeteer on Vercel and AWS Lambda
Vercel’s docs give the current recipe: puppeteer-core, which has no browser, and @sparticuz/chromium, a compressed Chromium built for serverless that is unpacked when the function first launches it. The binary in the package still counts toward the 250 MB limit; if the function goes over, Vercel’s large functions (beta, up to 5 GB) take it, or @sparticuz/chromium-min leaves the binary out and downloads it from a URL you host when a new instance starts, which makes that first request slower.
Pick the @sparticuz/chromium major that matches the Chromium of your puppeteer-core version; the package says breaking changes can come in a patch release. Give the function memory: @sparticuz/chromium recommends 1,600 MB or more, and Vercel’s default is 2 GB. The package only has Linux binaries, so on your Mac or Windows machine use a local Chrome. In Next.js, both packages are already in the default serverExternalPackages list.
// app/api/title/route.ts, from Vercel's own docs
import chromium from '@sparticuz/chromium';
import puppeteer from 'puppeteer-core';
export async function GET() {
const browser = await puppeteer.launch({
args: await puppeteer.defaultArgs({
args: chromium.args,
headless: 'shell',
}),
executablePath: await chromium.executablePath(),
headless: 'shell',
});
try {
const page = await browser.newPage();
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
return Response.json({ title: await page.title() });
} finally {
await browser.close();
}
}Slim Docker images: libraries and the sandbox
Chrome links against dozens of system libraries (NSS, GTK, ALSA, X11 libraries even in headless mode) that a slim image doesn’t have. ldd lists the ones it can’t find. Puppeteer’s troubleshooting page has the full Debian list, and its official image ghcr.io/puppeteer/puppeteer has them installed; its guide says to run it with --init.
Install fonts in the same step. A slim image has almost none, so text falls back to whatever is there, and accented or non-Latin characters can come out as boxes.
# Which libraries is Chrome missing? (run inside the production image)
ldd /path/to/chrome | grep "not found"
# Puppeteer can install them for its own Chrome on Debian or Ubuntu (root needed)
npx puppeteer browsers install chrome --install-deps
# The error on a slim image (puppeteer issue #9121):
# chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directoryProduction gotchas
- Fonts. Use
@font-facewith a file you control (or adata:URI) rather than relying on fonts installed on the server.@sparticuz/chromiumships only Open Sans. Puppeteer’spage.pdf()waits fordocument.fonts.readyby default. - Waiting.
setContentreturns atload. Images or data that arrive later needpage.waitForNetworkIdle(), or apage.waitForSelector()on an element your script adds when it’s done. Leaving the wait out is the usual cause of blank charts and missing logos. - Backgrounds.
printBackgrounddefaults tofalsein Puppeteer, so table stripes and coloured headers disappear until you turn it on. - Page size.
formatwins over CSS unless you passpreferCSSPageSize: true. Pick one place to set the paper. - Memory. Each open page adds a renderer process. Limit how many PDFs render at once (a queue, or a small pool of pages), and restart the browser if it crashes instead of launching one per request.
- Untrusted HTML. A browser with network access will load any URL in the HTML it’s given, including addresses inside your network. If users control the HTML, block requests with
page.setRequestInterception(true)or render somewhere with no network.
Print CSS that keeps pages intact
Chrome paginates by itself. These rules decide where it breaks, and they work the same in Puppeteer, Playwright and Canner’s renderer:
@page { size: letter; margin: 12mm; } /* Puppeteer and Playwright need preferCSSPageSize: true */
html { -webkit-print-color-adjust: exact; print-color-adjust: exact; } /* keep background colours */
thead { display: table-header-group; } /* repeat table headers on every page */
tr, .card { break-inside: avoid; } /* don't split a row or a block across pages */
.new-page { break-before: page; } /* force a page break */
@media print {
nav, .no-print { display: none; }
}When you don’t need HTML at all
If the document is a receipt, a label or a fixed form, a PDF library is lighter: no browser, no system libraries, no memory spike. pdfkit places text, lines and images; pdf-lib edits existing PDFs (fill a form, merge files, add a stamp). The cost is layout: you position everything yourself.
import fs from "node:fs";
import PDFDocument from "pdfkit";
// No HTML: you place the text yourself.
const doc = new PDFDocument({ size: "LETTER", margin: 50 });
doc.pipe(fs.createWriteStream("receipt.pdf"));
doc.fontSize(20).text("Receipt 1042");
doc.moveDown().fontSize(11).text("Hosting, November 2026: $9.00");
doc.end();Or skip the browser: Canner’s render service
On Canner, a Live project can turn on Document rendering (PDF) from its Services tab and redeploy. The app gets CANNER_RENDER_SOCKET, a unix socket: send one JSON line and the HTML, get one JSON line and the PDF back. No Chrome in your build, no libraries, no browser memory in your app. Copy the render() function for Node.js or Python from the docs.
- `pdf` preset. Paper
a3,a4,a5,letterorlegal(defaulta4),landscape,margin_mm0–50 (default 10),background(default on). - `screenshot` preset. A PNG,
widthandheight100–3000 (default 1280 × 800),scale1–3. - Self-contained HTML. The renderer has no network: put the CSS in a
<style>tag, and images and fonts indata:URIs. Links to outside stylesheets, fonts or images don’t load. Scripts run. This also means user-supplied HTML can’t reach anything. - Limits. 50 MB in, 200 MB out, 2 minutes a job, one job at a time per project and 20 a minute. Jobs use the organization’s 300 monthly job minutes, shared with media processing and OCR.
- Puppeteer on Canner. Builds install with
--ignore-scriptsand re-run allowed install scripts without network access, so thepuppeteerpackage can’t download Chrome there. The service is the way to render. - Office files too. The
convertpreset turns docx, xlsx, pptx, OpenDocument and RTF files into PDF with LibreOffice. - When it’s the wrong fit. You can’t pass Puppeteer options: no header and footer templates, no page that loads a live URL, no clicking through a login. If you need those, or many PDFs at the same moment, run Puppeteer yourself. Live projects only; on Démarrage, use pdfkit or pdf-lib.
An invoice, rendered by the service
The font and logo are read from disk and inlined as base64, so nothing in the HTML points outside the document. The table header repeats on each page and rows never split.
// invoice.mjs: the HTML carries its own font and logo, so it renders with no network
import fs from "node:fs";
import { render } from "./render.mjs"; // the render() function from /docs/services#render
const inline = (file, type) =>
`data:${type};base64,${fs.readFileSync(file).toString("base64")}`;
const FONT = inline("assets/Inter.woff2", "font/woff2");
const LOGO = inline("assets/logo.svg", "image/svg+xml");
const esc = (s) => String(s).replace(/[&<>"]/g, (ch) => `&#${ch.charCodeAt(0)};`);
const money = (cents) => "$" + (cents / 100).toFixed(2);
function invoiceHtml({ number, date, customer, lines }) {
const total = lines.reduce((sum, l) => sum + l.qty * l.cents, 0);
return `<!doctype html>
<html><head><meta charset="utf-8"><style>
@font-face { font-family: Inter; src: url(${FONT}) format("woff2"); }
body { font: 11pt Inter, sans-serif; color: #111; }
header { display: flex; justify-content: space-between; align-items: center; }
table { width: 100%; border-collapse: collapse; margin-top: 24px; }
th, td { padding: 6px 8px; border-bottom: 1px solid #ddd; text-align: left; }
td.n, th.n { text-align: right; }
thead { display: table-header-group; }
tr { break-inside: avoid; }
.total { background: #f1f5f9; font-weight: 600; }
</style></head><body>
<header><img src="${LOGO}" alt="Acme"><div>Invoice ${esc(number)}<br>${esc(date)}</div></header>
<p>Billed to: ${esc(customer)}</p>
<table>
<thead><tr><th>Item</th><th class="n">Qty</th><th class="n">Price</th><th class="n">Amount</th></tr></thead>
<tbody>${lines.map((l) => `<tr><td>${esc(l.item)}</td><td class="n">${l.qty}</td>
<td class="n">${money(l.cents)}</td><td class="n">${money(l.qty * l.cents)}</td></tr>`).join("")}
<tr class="total"><td colspan="3">Total</td><td class="n">${money(total)}</td></tr>
</tbody>
</table>
</body></html>`;
}
const html = invoiceHtml({
number: "1042",
date: "2026-11-25",
customer: "Tremblay & fils",
lines: [{ item: "Hosting, November", qty: 1, cents: 900 }],
});
const { data } = await render(Buffer.from(html), "pdf", { paper: "letter", margin_mm: 12 });
fs.writeFileSync("invoice-1042.pdf", data);
// A PNG preview of the same HTML:
// const { data: png } = await render(Buffer.from(html), "screenshot", { width: 1200, height: 900 });Questions
How do I fix “Could not find Chrome (ver. …)” in Puppeteer?
Install Chrome where the code runs: npx puppeteer browsers install chrome in the production image, or a .puppeteerrc.cjs with cacheDirectory inside the project so the browser ships with it. If you can’t, switch to puppeteer-core and pass executablePath to a Chrome or Chromium installed on the server.
How do I run Puppeteer on Vercel?
Use puppeteer-core with @sparticuz/chromium, as Vercel’s function docs show. The full puppeteer package’s Chrome is about 280 MB, over the 250 MB function limit. If @sparticuz/chromium still doesn’t fit, enable large functions or use @sparticuz/chromium-min with the binary hosted at a URL.
Is wkhtmltopdf still a good choice?
No for new work. Its GitHub repository is archived, the last release is from June 2020, and it runs on Qt WebKit, which Qt deprecated in 2015. Its own site warns against using it with untrusted HTML. The npm html-pdf package, built on PhantomJS, is deprecated and points to Puppeteer.
Why are my PDF backgrounds missing?
Puppeteer’s printBackground is false by default. Set it to true, and add print-color-adjust: exact in CSS if colours still shift. Canner’s pdf preset has background on by default.
Why do fonts or images not show up in the PDF?
The PDF was taken before they loaded, or the server can’t reach them. Wait with page.waitForNetworkIdle(), and prefer fonts and images inlined as data: URIs. With Canner’s renderer, inlining is required: it has no network.
Can I make a PDF in Node.js without a browser?
Yes, if you don’t need HTML: pdfkit draws a document from code and pdf-lib edits existing PDFs. Neither has native dependencies.
Render PDFs without shipping a browser.
Turn on document rendering, send HTML to a socket, and get a PDF back. Turn it on from the Services tab of any Live project.