DEV Community

codynaut
codynaut

Posted on Originally published at pagedpdf.com

Running Puppeteer on Vercel without the missing Chromium error

If Puppeteer on Vercel tells you the Chromium binary doesn't exist, you're not alone. This is the setup that got PDFs printing for me.

The regular puppeteer package downloads a full Chrome that won't fit in a Vercel Function. The usual fix is puppeteer-core with @sparticuz/chromium, a Chromium build made for AWS Lambda. Here's the setup that worked for me, plus the errors and limits I hit testing it.

Tested on Vercel with Next.js 16.4.3, puppeteer-core 25.11.0, @sparticuz/chromium 153.0.0 and Node 24.

The setup

npm install puppeteer-core @sparticuz/chromium
Enter fullscreen mode Exit fullscreen mode

Pin exact versions. The @sparticuz/chromium major version has to match the Chromium version your puppeteer-core release supports (153.0.0 pairs with 25.11.0, Chrome 153). The package doesn't follow semver, its README warns that breaking changes can land in patch releases, and it needs Node 22.17 or later.

// app/api/pdf/route.js
import chromium from '@sparticuz/chromium';
import puppeteer from 'puppeteer-core';

export const runtime = 'nodejs';
export const maxDuration = 60;

export async function POST(req) {
  const { html } = await req.json();
  const browser = await puppeteer.launch({
    args: await puppeteer.defaultArgs({ args: chromium.args, headless: 'shell' }),
    executablePath: process.env.VERCEL
      ? await chromium.executablePath()
      : process.env.LOCAL_CHROME_PATH, // the Lambda build won't run on your laptop
    headless: 'shell',
  });
  try {
    const page = await browser.newPage();
    await page.setContent(html, { waitUntil: 'load' });
    const pdf = await page.pdf({ format: 'A4', printBackground: true });
    return new Response(pdf, { headers: { 'Content-Type': 'application/pdf' } });
  } finally {
    await browser.close();
  }
}
Enter fullscreen mode Exit fullscreen mode

Fix the missing bin folder error

Deploy that as is and the build succeeds, but every request returns a 500:

Error: The input directory "/var/task/node_modules/@sparticuz/chromium/bin" does not exist.
Enter fullscreen mode Exit fullscreen mode

The Chromium bin folder isn't included in the function by default. Add it with outputFileTracingIncludes, one entry per route that launches Chromium:

// next.config.mjs
export default {
  outputFileTracingIncludes: {
    '/api/pdf': ['./node_modules/@sparticuz/chromium/bin/**'],
  },
};
Enter fullscreen mode Exit fullscreen mode

Older tutorials suggest serverExternalPackages. On Next.js 16.4.3, both an empty config and serverExternalPackages still returned the 500. Only outputFileTracingIncludes produced a PDF.

The limits that bite

  • Function size: 250 MB uncompressed. @sparticuz/chromium is about 70 MB, since Chromium stays compressed until a cold start.
  • Memory: the default is 2 GB with 1 vCPU. The package needs at least 512 MB and recommends 1,600 MB or more, so keep the default.
  • Request body: 4.5 MB. A 5 MB POST failed with 413 FUNCTION_PAYLOAD_TOO_LARGE. Embedded images and base64 fonts count toward it.
  • Response body: 4.5 MB, though streaming responses are exempt. A 26.5 MB PDF came back fine in my tests, but Vercel's docs don't say whether that counts as streaming, so I wouldn't rely on it. For big files, upload to Vercel Blob or S3 and return a link.
  • Duration: 300 s by default. Rarely matters for one document, but it does for batches.

Cold starts. On a cold start the package decompresses Chromium into /tmp before launching. For a one-page PDF I saw 3.3 to 4.5 s cold, about 0.45 s warm with a fresh launch, and about 0.25 s warm with a reused browser. Instances went cold after about 10 minutes idle.

Reusing the browser saves about 0.2 s per request. It does nothing for cold starts:

let browserPromise;
function getBrowser() {
  browserPromise ??= (async () => {
    const browser = await puppeteer.launch({ /* same options as above */ });
    browser.on('disconnected', () => { browserPromise = undefined; });
    return browser;
  })();
  return browserPromise;
}
// in the handler: const page = await (await getBrowser()).newPage(); ... finally { await page.close(); }
Enter fullscreen mode Exit fullscreen mode

@sparticuz/chromium-min leaves out the binary and downloads it from a URL you host on cold start. That keeps the bundle small but makes cold starts slower.

Why your fonts change in production

No server fonts. The Lambda environment has no fonts. The package adds only Open Sans (Latin, Greek and Cyrillic). Arial, Helvetica, Inter, Segoe UI, Times New Roman, Georgia and monospace all rendered as Open Sans, which also breaks code-block alignment. Emoji and Chinese, Japanese or Korean text just disappear, with no error.

Relative URLs. setContent has no base URL, so a relative @font-face URL fails and the PDF falls back to Times New Roman. Use an absolute URL or a <base href> tag.

The fix that works everywhere: embed the font as base64. Nothing is fetched at render time, and it doesn't depend on server fonts.

@font-face {
  font-family: "Brand";
  src: url(data:font/woff2;base64,AAAA...) format("woff2");
}
body { font-family: "Brand", sans-serif; }
Enter fullscreen mode Exit fullscreen mode

For emoji or CJK, add a Noto font to the list:

body { font-family: "Brand", "Noto Sans JP", "Noto Color Emoji", sans-serif; }
Enter fullscreen mode Exit fullscreen mode

A Japanese subset of Noto Sans JP (1.1 MB as WOFF2) worked as base64. Noto Color Emoji (5.5 MB) is over the 4.5 MB request limit before encoding, so load that one from an absolute URL. Order matters too: putting "Open Sans" ahead of Noto left Japanese blank every time.

You usually don't need your own font wait. page.pdf() waits for document.fonts.ready by default, so check the two causes above first.

Don't want to run Chromium in a serverless function? PagedPDF is an HTML-to-PDF API I built: one fetch call, fonts preinstalled, no cold start, and free for 50 PDFs a month.

// app/api/invoice/route.js
export async function GET() {
  const res = await fetch('https://api.pagedpdf.com/v1/pdf', {
    method: 'POST',
    headers: { Authorization: `Bearer ${process.env.PAGEDPDF_KEY}`, 'Content-Type': 'application/json' },
    body: JSON.stringify({ html: '<h1>Invoice 1042</h1>', footer: 'Page {page} of {pages}' }),
  });
  if (!res.ok) return new Response(await res.text(), { status: res.status });
  return new Response(res.body, { headers: { 'Content-Type': 'application/pdf' } });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)