Skip to content

Invoice

A parametric single-page invoice with an itemised services table, automatic VAT calculation, and a right-aligned totals block.

getParameterDefinitions exposes four controls — company name, client name, hourly rate, and hours worked. main() is async and returns a PDFDocument directly; the PDF backend awaits .save() automatically.

  • Right-aligned text using font.widthOfTextAtSize() to measure before drawing, so values line up regardless of their string length.
  • Column-based table layout with filled header and alternating row backgrounds built from drawRectangle and drawText.
  • Colour palette defined once and reused throughout for consistency.
Parameter Effect
Company Name shown in the header bar and line-item description
Client Billed-to name
Rate Per-hour fee; drives subtotal, VAT, and total
Hours Quantity of hours; multiplied by rate
"use strict";
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib');
const W = 595, H = 842, M = 50;
function getParameterDefinitions() {
return [
{ name: 'company', type: 'text', default: 'Acme Corp', caption: 'Company name' },
{ name: 'client', type: 'text', default: 'Client Ltd', caption: 'Client name' },
{ name: 'rate', type: 'number', widget: 'slider', default: 150, min: 50, max: 500, step: 10, caption: 'Hourly rate (£)' },
{ name: 'hours', type: 'number', widget: 'slider', default: 8, min: 1, max: 80, step: 1, caption: 'Hours worked' },
];
}
async function main({ company, client, rate, hours }) {
const doc = await PDFDocument.create();
const bold = await doc.embedFont(StandardFonts.HelveticaBold);
const reg = await doc.embedFont(StandardFonts.Helvetica);
const page = doc.addPage([W, H]);
const subtotal = rate * hours;
const tax = subtotal * 0.20;
const total = subtotal + tax;
// ── Colours ───────────────────────────────────────────────────────────────
const navy = rgb(0.12, 0.24, 0.49);
const stripe = rgb(0.94, 0.96, 1.00);
const muted = rgb(0.50, 0.50, 0.50);
const dark = rgb(0.12, 0.12, 0.12);
const white = rgb(1, 1, 1);
// ── Header bar ────────────────────────────────────────────────────────────
page.drawRectangle({ x: 0, y: H - 78, width: W, height: 78, color: navy });
page.drawText(company, { x: M, y: H - 48, size: 22, font: bold, color: white });
const invLabel = 'INVOICE';
page.drawText(invLabel, {
x: W - M - bold.widthOfTextAtSize(invLabel, 26),
y: H - 50,
size: 26, font: bold, color: white,
});
// ── Billing block ─────────────────────────────────────────────────────────
const c2 = W - M - 160; // right-column x
let y = H - 115;
function label(text, x) { page.drawText(text, { x, y, size: 9, font: bold, color: muted }) }
function value(text, x) { page.drawText(text, { x, y, size: 11, font: reg, color: dark }) }
label('Bill to:', M); label('Invoice:', c2); y -= 15;
value(client, M); value('INV-001', c2); y -= 16;
label('Date:', c2); y -= 14;
value('1 Jan 2026', c2); y -= 16;
label('Due:', c2); y -= 14;
value('31 Jan 2026', c2);
// ── Table ─────────────────────────────────────────────────────────────────
y = H - 300;
const cols = { desc: M, qty: 320, unit: 400, amt: 490 };
const tableW = W - M * 2;
const rowH = 28;
// Header row
page.drawRectangle({ x: M, y: y - 5, width: tableW, height: rowH, color: navy });
[['Description', cols.desc + 6], ['Qty', cols.qty + 6], ['Unit price', cols.unit + 6], ['Amount', cols.amt + 6]].forEach(([t, x]) =>
page.drawText(t, { x, y: y + 8, size: 10, font: bold, color: white }),
);
// Data row
y -= rowH;
page.drawRectangle({ x: M, y: y - 5, width: tableW, height: rowH, color: stripe });
page.drawText(`Professional services — ${company}`, { x: cols.desc + 6, y: y + 8, size: 10, font: reg, color: dark });
page.drawText(`${hours}h`, { x: cols.qty + 6, y: y + 8, size: 10, font: reg, color: dark });
page.drawText(`£${rate.toFixed(2)}`, { x: cols.unit + 6, y: y + 8, size: 10, font: reg, color: dark });
page.drawText(`£${subtotal.toFixed(2)}`, { x: cols.amt + 6, y: y + 8, size: 10, font: reg, color: dark });
// ── Totals ────────────────────────────────────────────────────────────────
y -= rowH + 24;
const totX = cols.unit - 10;
function totRow(lbl, val, isBold) {
const f = isBold ? bold : reg;
const clr = isBold ? navy : rgb(0.3, 0.3, 0.3);
page.drawText(lbl, { x: totX, y, size: 11, font: f, color: clr });
page.drawText(val, { x: W - M - f.widthOfTextAtSize(val, 11), y, size: 11, font: f, color: clr });
y -= 20;
}
totRow('Subtotal', `£${subtotal.toFixed(2)}`, false);
totRow('VAT (20%)', `£${tax.toFixed(2)}`, false);
page.drawLine({ start: { x: totX, y: y + 14 }, end: { x: W - M, y: y + 14 }, thickness: 1, color: navy });
totRow('Total due', `£${total.toFixed(2)}`, true);
// ── Footer ────────────────────────────────────────────────────────────────
page.drawLine({ start: { x: M, y: 58 }, end: { x: W - M, y: 58 }, thickness: 0.5, color: rgb(0.8, 0.8, 0.8) });
page.drawText('Thank you for your business.', { x: M, y: 42, size: 9, font: reg, color: muted });
const pg = 'Page 1 of 1';
page.drawText(pg, { x: W - M - reg.widthOfTextAtSize(pg, 9), y: 42, size: 9, font: reg, color: muted });
return doc;
}
module.exports = { main, getParameterDefinitions };