$npx -y skills add sabahattink/antigravity-fullstack-hq --skill pdf-officialGenerating PDFs with pdfkit or puppeteer in Node.js. Use when generating PDF reports, invoices, certificates, or any printable document from data or HTML templates.
| 1 | # PDF Generation |
| 2 | |
| 3 | ## Option A — PDFKit (Programmatic) |
| 4 | |
| 5 | Best for: invoices, receipts, structured data documents with precise control. |
| 6 | |
| 7 | ```bash |
| 8 | npm install pdfkit |
| 9 | npm install -D @types/pdfkit |
| 10 | ``` |
| 11 | |
| 12 | ```typescript |
| 13 | // lib/pdf/invoice-pdf.ts |
| 14 | import PDFDocument from 'pdfkit' |
| 15 | import { PassThrough } from 'stream' |
| 16 | |
| 17 | export async function generateInvoicePdf(invoice: Invoice): Promise<Buffer> { |
| 18 | return new Promise((resolve, reject) => { |
| 19 | const doc = new PDFDocument({ margin: 50, size: 'A4' }) |
| 20 | const chunks: Buffer[] = [] |
| 21 | |
| 22 | doc.on('data', chunk => chunks.push(chunk)) |
| 23 | doc.on('end', () => resolve(Buffer.concat(chunks))) |
| 24 | doc.on('error', reject) |
| 25 | |
| 26 | // ─── Header ────────────────────────────────────────────── |
| 27 | doc |
| 28 | .fillColor('#2563EB') |
| 29 | .fontSize(28) |
| 30 | .font('Helvetica-Bold') |
| 31 | .text('INVOICE', 50, 50) |
| 32 | |
| 33 | doc |
| 34 | .fillColor('#6B7280') |
| 35 | .fontSize(10) |
| 36 | .font('Helvetica') |
| 37 | .text(`Invoice #${invoice.number}`, 50, 90) |
| 38 | .text(`Date: ${invoice.date.toLocaleDateString('en-US')}`, 50, 105) |
| 39 | .text(`Due: ${invoice.dueDate.toLocaleDateString('en-US')}`, 50, 120) |
| 40 | |
| 41 | // Company info on the right |
| 42 | doc |
| 43 | .fillColor('#111827') |
| 44 | .fontSize(10) |
| 45 | .text('Antigravity HQ', 400, 50, { align: 'right' }) |
| 46 | .text('123 Tech Street', 400, 65, { align: 'right' }) |
| 47 | .text('contact@antigravity.dev', 400, 80, { align: 'right' }) |
| 48 | |
| 49 | // ─── Divider ───────────────────────────────────────────── |
| 50 | doc |
| 51 | .strokeColor('#E5E7EB') |
| 52 | .lineWidth(1) |
| 53 | .moveTo(50, 145) |
| 54 | .lineTo(545, 145) |
| 55 | .stroke() |
| 56 | |
| 57 | // ─── Bill To ───────────────────────────────────────────── |
| 58 | doc |
| 59 | .fillColor('#374151') |
| 60 | .fontSize(9) |
| 61 | .font('Helvetica-Bold') |
| 62 | .text('BILL TO', 50, 160) |
| 63 | .font('Helvetica') |
| 64 | .text(invoice.client.name, 50, 175) |
| 65 | .text(invoice.client.address, 50, 190) |
| 66 | .text(invoice.client.email, 50, 205) |
| 67 | |
| 68 | // ─── Table Header ───────────────────────────────────────── |
| 69 | const tableTop = 250 |
| 70 | doc |
| 71 | .fillColor('#2563EB') |
| 72 | .rect(50, tableTop, 495, 22) |
| 73 | .fill() |
| 74 | .fillColor('#FFFFFF') |
| 75 | .font('Helvetica-Bold') |
| 76 | .fontSize(9) |
| 77 | .text('Description', 60, tableTop + 7) |
| 78 | .text('Qty', 330, tableTop + 7, { width: 50, align: 'right' }) |
| 79 | .text('Unit Price', 385, tableTop + 7, { width: 70, align: 'right' }) |
| 80 | .text('Amount', 460, tableTop + 7, { width: 80, align: 'right' }) |
| 81 | |
| 82 | // ─── Table Rows ─────────────────────────────────────────── |
| 83 | let y = tableTop + 30 |
| 84 | invoice.items.forEach((item, i) => { |
| 85 | if (i % 2 === 0) { |
| 86 | doc.fillColor('#F8FAFC').rect(50, y - 4, 495, 22).fill() |
| 87 | } |
| 88 | doc |
| 89 | .fillColor('#374151') |
| 90 | .font('Helvetica') |
| 91 | .fontSize(9) |
| 92 | .text(item.description, 60, y) |
| 93 | .text(String(item.quantity), 330, y, { width: 50, align: 'right' }) |
| 94 | .text(`$${item.unitPrice.toFixed(2)}`, 385, y, { width: 70, align: 'right' }) |
| 95 | .text(`$${(item.quantity * item.unitPrice).toFixed(2)}`, 460, y, { width: 80, align: 'right' }) |
| 96 | |
| 97 | y += 22 |
| 98 | }) |
| 99 | |
| 100 | // ─── Total ──────────────────────────────────────────────── |
| 101 | doc |
| 102 | .strokeColor('#E5E7EB') |
| 103 | .lineWidth(1) |
| 104 | .moveTo(380, y + 5) |
| 105 | .lineTo(545, y + 5) |
| 106 | .stroke() |
| 107 | |
| 108 | doc |
| 109 | .fillColor('#111827') |
| 110 | .font('Helvetica-Bold') |
| 111 | .fontSize(11) |
| 112 | .text('Total:', 380, y + 15) |
| 113 | .text(`$${invoice.total.toFixed(2)}`, 460, y + 15, { width: 80, align: 'right' }) |
| 114 | |
| 115 | doc.end() |
| 116 | }) |
| 117 | } |
| 118 | ``` |
| 119 | |
| 120 | ## Option B — Puppeteer (HTML → PDF) |
| 121 | |
| 122 | Best for: complex layouts, charts, styled reports that are easier to build in HTML/CSS. |
| 123 | |
| 124 | ```bash |
| 125 | npm install puppeteer |
| 126 | # For production Docker: use puppeteer-core + system Chrome |
| 127 | npm install puppeteer-core |
| 128 | ``` |
| 129 | |
| 130 | ```typescript |
| 131 | // lib/pdf/html-pdf.ts |
| 132 | import puppeteer from 'puppeteer' |
| 133 | |
| 134 | export async function htmlToPdf(html: string, options?: { |
| 135 | format?: 'A4' | 'Letter' |
| 136 | landscape?: boolean |
| 137 | margin?: { top: string; right: string; bottom: string; left: string } |
| 138 | }): Promise<Buffer> { |
| 139 | const browser = await puppeteer.launch({ |
| 140 | headless: true, |
| 141 | args: [ |
| 142 | '--no-sandbox', |
| 143 | '--disable-setuid-sandbox', |
| 144 | '--disable-dev-shm-usage', // required in Docker |
| 145 | ], |
| 146 | }) |
| 147 | |
| 148 | try { |
| 149 | const page = await browser.newPage() |
| 150 | |
| 151 | await page.setContent(html, { waitUntil: 'networkidle0' }) |
| 152 | |
| 153 | const buffer = await page.pdf({ |
| 154 | format: options?.format ?? 'A4', |
| 155 | landscape: options?.landscape ?? false, |
| 156 | margin: options?.margin ?? { top: '20mm', right: '15mm', bottom: '20mm', left: '15mm' }, |
| 157 | printBackground: true, |
| 158 | }) |
| 159 | |
| 160 | return Buffer.from(buffer) |
| 161 | } finally { |
| 162 | await browser.close() |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // HTML template for a report |
| 167 | function buildReportHtml(data: ReportData): stri |