The PDF Generation Engineer produces server-side documents - invoices, contracts, reports - that match the brand and render correctly across PDF viewers. PDF generation has two viable approaches: HTML-to-PDF via headless browser (flexible, matches your web styles) and programmatic PDF construction via React-PDF or PDFKit (no browser dependency, faster for high volume).
Approach Comparison
| Approach | Best for | Cons |
|---|---|---|
| Puppeteer / Playwright | Pixel-perfect match to existing web designs, complex layouts | Heavy (~170MB Chrome binary), slow cold start in serverless |
| React-PDF | Structured documents like invoices, contracts; no browser needed | Limited CSS support; custom layout primitives, not HTML |
| PDFKit (Node) | Low-level, high-control; large volumes | No HTML; very verbose for complex layouts |
| External service | Browserless.io, Gotenberg; offload headless infrastructure | Network latency; cost; external dependency |
Puppeteer PDF (HTML-to-PDF)
import puppeteer from 'puppeteer-core';
import chromium from '@sparticuz/chromium'; // Lambda-compatible Chromium
export async function generatePdf(html: string): Promise<Buffer> {
const browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath(),
headless: chromium.headless,
});
const page = await browser.newPage();
// Set content and wait for fonts/images to load
await page.setContent(html, { waitUntil: 'networkidle0' });
const pdf = await page.pdf({
format: 'A4',
printBackground: true, // Include background colours from CSS
margin: { top: '20mm', right: '20mm', bottom: '20mm', left: '20mm' },
});
await browser.close();
return Buffer.from(pdf);
}
// Return as download in Next.js API route
export async function GET() {
const html = renderToStaticMarkup(<InvoiceTemplate data={data} />);
const pdf = await generatePdf(`<!DOCTYPE html>${html}`);
return new Response(pdf, {
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename="invoice-2026-001.pdf"',
},
});
}
React-PDF Invoice Component
import { Document, Page, Text, View, StyleSheet, Font } from '@react-pdf/renderer';
Font.register({ family: 'Inter', src: '/fonts/Inter-Regular.ttf' });
const styles = StyleSheet.create({
page: { fontFamily: 'Inter', padding: 40, fontSize: 10, color: '#111827' },
header: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 32 },
title: { fontSize: 24, fontWeight: 'bold', color: '#3b82f6' },
table: { width: '100%', marginTop: 16 },
row: { flexDirection: 'row', borderBottom: '1pt solid #e5e7eb', paddingVertical: 6 },
total: { flexDirection: 'row', justifyContent: 'flex-end', marginTop: 16 },
});
export const Invoice = ({ invoice }) => (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.header}>
<Text style={styles.title}>INVOICE</Text>
<Text>#{invoice.number}</Text>
</View>
{/* line items ... */}
<View style={styles.total}>
<Text>Total: ${invoice.total.toFixed(2)}</Text>
</View>
</Page>
</Document>
);