Generate PDFs in Next.js without Puppeteer

In Next.js, generate a PDF in a route handler by sending your data as JSON to the Quire API and returning the bytes with a Content-Type of application/pdf. It takes no headless Chrome, no HTML template and no extra serverless memory, and it works on Vercel, on any Node.js host and in the Edge runtime.

Running Puppeteer or Playwright inside a Next.js app means shipping a browser with your functions, raising memory limits and fighting cold starts. With Quire, the route handler sends JSON and streams back the PDF.

1. Install and get a key

npm install quirepdf
npx quirepdf-cli login [email protected]

Add the key to .env.local (and to your host’s environment variables):

QUIRE_API_KEY=qk_live_...

2. Add a route handler

app/invoices/[id]/pdf/route.ts:

import { Quire, QuireError, type InvoiceData } from "quirepdf";
import { getInvoice } from "@/lib/invoices"; // your data access

const quire = new Quire(); // reads QUIRE_API_KEY

export async function GET(_req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const inv = await getInvoice(id);
  if (!inv) return new Response("Not found", { status: 404 });

  const data: InvoiceData = {
    number: inv.number,
    status: inv.paidAt ? "paid" : "due",
    issued: inv.issuedAt.toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric" }),
    due: inv.dueAt.toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric" }),
    currency: "$",
    seller: { name: "Northwind Studio", address: ["221 Market Street", "San Francisco, CA 94105"] },
    customer: { name: inv.customer.name, address: inv.customer.addressLines, email: inv.customer.email },
    items: inv.lines.map((l) => ({ description: l.description, qty: l.quantity, unit_price: l.unitPrice })),
    tax: inv.taxRate ? { label: `Tax (${Math.round(inv.taxRate * 1000) / 10}%)`, rate: inv.taxRate } : undefined,
  };

  try {
    const pdf = await quire.render(data, { template: "invoice" });
    return new Response(pdf.bytes.slice(), { // .slice(): the body type recent TypeScript expects
      headers: {
        "Content-Type": "application/pdf",
        "Content-Disposition": `inline; filename="${inv.number}.pdf"`,
      },
    });
  } catch (e) {
    if (e instanceof QuireError) return Response.json({ error: e.message, fields: e.fields }, { status: 502 });
    throw e;
  }
}

Open /invoices/123/pdf in the browser and the PDF shows inline. Change inline to attachment to force a download.

The invoice template computes line totals, tax and the grand total from qty, unit_price and tax.rate, so you don’t format or add up money yourself. InvoiceData is typed, so a missing customer.name or a string qty fails at compile time.

3. Develop with free test renders

While building, pass test: true. You get the same PDF with a watermark, free and not counted against your plan:

const pdf = await quire.render(data, { template: "invoice", test: process.env.NODE_ENV !== "production" });

A thumbnail for the invoice page

Render page 1 as a PNG for a preview image:

const png = await quire.render(data, { template: "invoice", format: "png", page: 1 });
return new Response(png.bytes.slice(), { headers: { "Content-Type": "image/png" } });

Documents that aren’t invoices

You don’t need a template. Send any JSON object and Quire lays it out: title, from and to make the header, short fields go in a details panel, and arrays of objects become tables.

const pdf = await quire.render({
  title: "Monthly usage report",
  date: "October 2026",
  currency: "$",
  summary: { active_users: 1284, renders: 48210, revenue: 9120.5 },
  top_customers: [{ name: "Acme", renders: 9120 }, { name: "Lumen", renders: 6400 }],
});

Server Actions

The same call works in a Server Action, for example to email a receipt after checkout. Keep the key on the server: never call the API with your key from client components.

Next steps