Stack · Netlify Functions
Generate PDFs in Netlify Functions
Netlify Functions are AWS Lambda underneath, which means the same bundle ceiling: a Chromium build consumes most of it before your own code is included, and cold starts grow accordingly.
Keep the function tiny. Build the HTML from your data, POST it, and return the PDF — the deploy stays fast and the bundle stays measured in kilobytes.
01Call it from Netlify Functions
// netlify/functions/invoice.mjs
export default async (req, context) => {
const { invoiceId } = await req.json();
const html = await buildInvoiceHtml(invoiceId);
const res = await fetch("https://rendral.com/api/v1/pdf", {
method: "POST",
headers: {
Authorization: "Bearer " + Netlify.env.get("RENDRAL_API_KEY"),
"Content-Type": "application/json",
},
body: JSON.stringify({
html,
options: { format: "A4", pageNumbers: true },
}),
});
if (!res.ok) {
return new Response("Render failed", { status: 502 });
}
return new Response(res.body, {
headers: {
"Content-Type": "application/pdf",
"Content-Disposition": 'inline; filename="invoice.pdf"',
},
});
};Frequently asked
Can't I just bundle Chromium?
You can try, but a Chromium build consumes most of the 50MB zipped limit before your code is added, and it lengthens every cold start. It's rarely worth it for document rendering.
Does this work in Edge Functions too?
Yes. Edge Functions run on Deno and definitely can't spawn a browser, so an outbound fetch is the only workable approach there.
Where do environment variables go?
Set them in Site configuration → Environment variables, then read them with Netlify.env.get in modern functions or process.env in the classic handler.
How do I avoid the 10-second timeout?
A single render finishes well inside it. For long or bulk documents, send async: true and receive a webhook instead of holding the function open.
Related
Shipping labels PDF API
Generate 4x6 thermal shipping labels and packing slips as PDFs from HTML.
Compare vs self-hosted Puppeteer
How self-hosted Puppeteer and Rendral differ on pricing, features, and setup.
PDFs in Next.js
A step-by-step guide to generating PDFs from Next.js.
A free tier, five endpoints, and SDKs for Node and Python.