JavaScript

Generate PDFs in Next.js

Render PDFs from a Next.js Route Handler or API route and stream them straight to the browser.

In Next.js the clean place to generate a PDF is a server-side Route Handler (App Router) or API route — your PDFgen key stays on the server, and you stream the PDF straight back to the browser.

The handler below POSTs your HTML and data to PDFgen and returns the PDF with the right content type, so a link or fetch to it downloads the file. No Puppeteer to bundle into your serverless functions.

Generate a PDF in Next.js

POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.

// app/api/invoice/route.ts
export async function GET() {
const res = await fetch("https://pdfgen.com/api/v1/generate", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.PDFGEN_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
html: "<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>",
engine: "handlebars",
format: "A4",
data: { number: "INV-001", client: "Acme Corp", total: "$2,400.00" },
}),
});
return new Response(await res.arrayBuffer(), {
headers: {
"Content-Type": "application/pdf",
"Content-Disposition": "inline; filename=invoice.pdf",
},
});
}

Convert existing HTML to PDF in Next.js

Already have finished HTML? Skip templating — POST HTML to a Route Handler and stream the PDF back and send it with engine: "legacy", which tells the API your markup is final.

// app/api/html-to-pdf/route.ts
export async function POST(req: Request) {
const html = await req.text(); // the HTML you want to convert
const res = await fetch("https://pdfgen.com/api/v1/generate", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.PDFGEN_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ html, engine: "legacy", format: "A4" }),
});
return new Response(await res.arrayBuffer(), {
headers: { "Content-Type": "application/pdf" },
});
}

How it works

  1. 1

    Send your HTML + data

    POST an HTML template (with optional Handlebars tokens) and a data object to the API.

  2. 2

    We render the PDF

    PDFgen renders it server-side — no headless browser for you to run or scale.

  3. 3

    Get the PDF back

    Receive the PDF binary (or a hosted link with export_type:"url") and serve it from Next.js.

Good to know

  • Put the call in a Route Handler / API route so PDFGEN_API_KEY never reaches the client bundle.
  • Returning the arrayBuffer with a PDF content type lets the browser preview or download it directly.
  • No headless Chrome to bundle — this stays well under serverless function size limits.

Frequently asked questions

Where should the PDF call live in Next.js?
In a server-side Route Handler (app/api/.../route.ts) or pages/api route. That keeps your API key on the server and avoids shipping it to the browser.
Can I generate the PDF in a Server Action?
Yes — call /api/v1/generate from a Server Action the same way; return the bytes or upload them, since you can’t stream a file download directly from an action.
Does this work on Vercel / serverless?
Yes. Because rendering happens on PDFgen, there’s no headless browser to bundle, so it stays within serverless size and memory limits.

Generate PDFs in other languages

Your first PDF from Next.js in five minutes

Grab an API key, paste the snippet above, and render a PDF — no headless browsers to babysit. Build a reusable template with AI when you're ready.