import { readFile } from 'node:fs/promises'; import puppeteer from 'puppeteer'; const [input, output = 'invoice.pdf'] = process.argv.slice(2); if (!input) throw new Error('Pass an invoice JSON file and an optional PDF filename.'); const data = JSON.parse(await readFile(input, 'utf8')); const escape = value => String(value).replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', }[char])); const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }); const isoDate = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeZone: 'UTC' }); const total = data.items.reduce((sum, item) => sum + item.quantity * Math.round(item.unitPrice * 100), 0); const rows = data.items.map(({ description, quantity, unitPrice }) => `
NORTHSTAR STUDIO
${escape(data.invoice.number)} / ${isoDate.format(new Date(data.invoice.date))}
Payment due ${isoDate.format(new Date(data.invoice.due))}
BILL TO
${escape(data.client.name)}
${escape(data.client.address)}
| Description | Qty | Unit price | Amount |
|---|
Total ${money.format(total / 100)}
${escape(data.notes)}