DEV Community

One banquet event order, three PDFs: client pricing, kitchen prep, and revision diff

The client approved dinner for 80. The count goes up to 86, the dessert tray is canceled, and 12 late-night snacks are added. The kitchen needs the new counts and prep instructions. The client needs the revised total of $2,805.96. The event lead needs a record of what changed and why the bill went up by $189.95.

Calculate the invoice in cents

Store each quantity and unit price as an integer. Multiply first, sum the line extensions, and round each percentage charge once on its agreed base. This example applies an 18% service charge to the subtotal and 8.25% sample tax to the subtotal plus service. Positive charges round half up to a cent. The taxable base and rounding points are inputs from the contract and invoicing rules for the event.

Give each recipient a fresh PDF

Project the event into kitchen rows containing quantities, item names, and prep notes before rendering. Keep prep notes in an operations-only field. A black rectangle over a price leaves the original PDF text available for extraction. A new document built from the kitchen rows contains only the content that was drawn into it. pdf-lib PDFDocument API

Save the block as beo.mts

In a scratch directory, install the dependency with npm install pdf-lib@1.17.1, then run node beo.mts using Node.js 24. The script writes three PDFs and checks the calculation, the kitchen projection, and the serialized page counts. Node.js TypeScript support

import assert from "node:assert/strict";
import { writeFile } from "node:fs/promises";
import { PDFDocument, StandardFonts } from "pdf-lib";

type Line = { id: string; item: string; quantity: bigint; unitCents: bigint; prep: string; };
type Revision = { eventId: string; version: number; lines: Line[] };

function usd(cents: bigint): string {
  const absolute = cents < 0n ? -cents : cents;
  return `${cents < 0n ? "-" : ""}$${absolute / 100n}.${String(absolute % 100n).padStart(2, "0")}`;
}

const extended = (line: Line) => line.quantity * line.unitCents;
const roundCharge = (base: bigint, basisPoints: bigint) => (base * basisPoints + 5_000n) / 10_000n;

function byId(lines: Line[]): Map<string, Line> {
  const indexed = new Map(lines.map((line) => [line.id, line]));
  assert.equal(indexed.size, lines.length, "Line IDs must be unique within a revision");
  for (const line of lines) {
    assert(line.quantity > 0n && line.unitCents >= 0n, "Expected nonnegative sale prices and positive quantities");
  }
  return indexed;
}

function totals(lines: Line[]) {
  const subtotal = lines.reduce((sum, line) => sum + extended(line), 0n);
  const service = roundCharge(subtotal, 1_800n);
  const tax = roundCharge(subtotal + service, 825n);
  return { subtotal, service, tax, total: subtotal + service + tax };
}

function kitchenRows(lines: Line[]): string[] {
  return lines.map(({ item, quantity, prep }) => `${quantity} x ${item} | ${prep}`);
}

function revisionRows(previous: Revision, current: Revision): string[] {
  assert.equal(previous.eventId, current.eventId);
  assert(current.version > previous.version, "Compare the earlier revision first");
  const before = byId(previous.lines);
  const after = byId(current.lines);
  const rows: string[] = [];
  for (const id of new Set([...before.keys(), ...after.keys()])) {
    const oldLine = before.get(id);
    const newLine = after.get(id);
    if (!oldLine && newLine) {
      rows.push(`Added ${newLine.item}: ${newLine.quantity} (${usd(extended(newLine))})`);
    } else if (oldLine && !newLine) {
      rows.push(`Removed ${oldLine.item}: ${oldLine.quantity} (${usd(-extended(oldLine))})`);
    } else if (oldLine && newLine) {
      if (oldLine.item !== newLine.item) rows.push(`Item: ${oldLine.item} -> ${newLine.item}`);
      if (oldLine.quantity !== newLine.quantity) {
        rows.push(`${newLine.item} quantity: ${oldLine.quantity} -> ${newLine.quantity}`);
      }
      if (oldLine.unitCents !== newLine.unitCents) {
        rows.push(`${newLine.item} unit price: ${usd(oldLine.unitCents)} -> ${usd(newLine.unitCents)}`);
      }
      if (oldLine.prep !== newLine.prep) rows.push(`New ${newLine.item} prep: ${newLine.prep}`);
    }
  }
  const oldTotals = totals(previous.lines);
  const newTotals = totals(current.lines);
  rows.push(`Subtotal change: ${usd(newTotals.subtotal - oldTotals.subtotal)}`);
  rows.push(`Service change: ${usd(newTotals.service - oldTotals.service)}`);
  rows.push(`Tax change: ${usd(newTotals.tax - oldTotals.tax)}`);
  rows.push(`Invoice change: ${usd(newTotals.total - oldTotals.total)}`);
  return rows;
}

async function savePdf(filename: string, title: string, rows: string[]) {
  const doc = await PDFDocument.create();
  const font = await doc.embedFont(StandardFonts.Helvetica);
  doc.setTitle(title);
  for (let offset = 0; offset < rows.length; offset += 30) {
    const page = doc.addPage([612, 792]);
    page.drawText(title, { x: 48, y: 744, font, size: 18 });
    rows.slice(offset, offset + 30).forEach((row, i) => {
      assert(font.widthOfTextAtSize(row, 11) <= 516, "Row is too wide for this fixture");
      page.drawText(row, { x: 48, y: 704 - i * 20, font, size: 11 });
    });
  }
  const bytes = await doc.save();
  assert.equal((await PDFDocument.load(bytes)).getPageCount(), Math.ceil(rows.length / 30));
  await writeFile(filename, bytes);
}

const original: Revision = {
  eventId: "BEO-042",
  version: 1,
  lines: [
    { id: "dinner", item: "Dinner", quantity: 80n, unitCents: 2495n, prep: "Serve at 18:30" },
    { id: "dessert", item: "Dessert tray", quantity: 8n, unitCents: 650n, prep: "Set out at 20:00" },
  ],
};

const current: Revision = {
  ...original,
  version: 2,
  lines: [
    { ...original.lines[0], quantity: 86n, prep: "Serve at 18:45" },
    { id: "snacks", item: "Snack box", quantity: 12n, unitCents: 425n, prep: "Set out at 21:00" },
  ],
};

const bill = totals(current.lines);
const prep = kitchenRows(current.lines);
const changes = revisionRows(original, current);

assert.deepEqual(bill, { subtotal: 219670n, service: 39541n, tax: 21385n, total: 280596n });
assert.deepEqual(prep, ["86 x Dinner | Serve at 18:45", "12 x Snack box | Set out at 21:00"]);
assert(changes.includes("Dinner quantity: 80 -> 86"));
assert(changes.includes("Removed Dessert tray: 8 (-$52.00)"));
assert(changes.includes("Added Snack box: 12 ($51.00)"));
assert(changes.includes("Invoice change: $189.95"));

const label = `${current.eventId} | R${current.version}`;
await savePdf("beo-client.pdf", `${label} | Client`, [
  ...current.lines.map((line) => `${line.quantity} x ${line.item} at ${usd(line.unitCents)} = ${usd(extended(line))}`),
  `Subtotal: ${usd(bill.subtotal)}`,
  `Service: ${usd(bill.service)}`,
  `Tax: ${usd(bill.tax)}`,
  `Total: ${usd(bill.total)}`,
]);
await savePdf("beo-kitchen.pdf", `${label} | Kitchen`, prep);
await savePdf("beo-revision.pdf", `${current.eventId} | R1 to R2 | Internal`, changes);
console.log(`Wrote 3 PDFs. Client total ${usd(bill.total)}. ${changes.at(-1)}.`);

Stable IDs let a moved or renamed line retain its history. The revision report shows a $148.70 subtotal increase, a $26.77 service increase, and a $14.48 tax increase. Keep that priced report with the event lead.

Check the kitchen PDF with a text extractor such as pdftotext and confirm that the service time and counts survived the export. The fixture uses Helvetica and short rows. Wrap longer notes and embed a font with the needed glyphs for a real event packet.

BEOPack is published by the team behind this article. It brings the client BEO, kitchen prep sheet, and revision diff together in one event packet workflow. BEOPack AI disclosure. This article was generated with AI.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.