feat: render billing usage pdf
Add the immutable PDF renderer for "Nutzungsnachweis" billing reports.
- src/modules/billing/types.ts: BillingReportSnapshot self-contained type
(address, selected positions, repeat downloads, net total in cents,
report/invoice metadata, period, document ID, legal notice).
- src/modules/pdf/document.ts: pdfmake document definition builder with
monochrome invoice layout (black logo, sender/recipient blocks, rounded
black summary panel, repeated black table headers, light-gray rows,
German dates, cents-derived totals, page numbers, document ID, invoice
reference, legal separation notice).
- src/modules/pdf/render.ts: renderBillingPdf → { bytes, sha256 } using
pdfmake 0.3 with IBM Plex Mono (OFL) embedded via the VFS.
- scripts/render-pdf-fixture.ts: stress fixture generator (1 row, 60 rows,
long address, long fields, umlauts, 3-page case) into tmp/pdfs/.
- tests/fixtures/billing-report.ts: deterministic reportFixture (60,00 EUR).
- tsconfig.json: server-only path alias for tsx script execution.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
d37affc80f
commit
89d76e2711
13 files changed
+1265
-1
No files matched your search
@@ -0,0 +1,218 @@
|
||||
#!/usr/bin/env tsx
|
||||
/**
|
||||
* Renders stress-test PDFs from BillingReportSnapshot fixtures into tmp/pdfs/.
|
||||
*
|
||||
* Covers: 1 row, 60 rows, long German company/address text, long
|
||||
* filenames/purposes, umlauts (äöüß), and a 3-page case.
|
||||
*
|
||||
* Run: pnpm tsx scripts/render-pdf-fixture.ts
|
||||
*
|
||||
* PNG visual inspection (pdftoppm) is deferred to Task 15 — this script only
|
||||
* generates the PDFs. When pdftoppm is available, run:
|
||||
* pdftoppm -png tmp/pdfs/billing-stress.pdf tmp/pdfs/billing-stress
|
||||
*/
|
||||
|
||||
import { promises as fs } from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import type { BillingReportSnapshot } from "../src/modules/billing/types";
|
||||
import { renderBillingPdf } from "../src/modules/pdf/render";
|
||||
|
||||
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "pdfs");
|
||||
|
||||
function baseAddress() {
|
||||
return {
|
||||
companyName: "Redaktion Beispiel GmbH",
|
||||
contactName: "Anna Schmidt",
|
||||
addressLine1: "Musterstraße 12",
|
||||
addressLine2: null as string | null,
|
||||
postalCode: "10115",
|
||||
city: "Berlin",
|
||||
countryCode: "DE",
|
||||
};
|
||||
}
|
||||
|
||||
function basePosition(priceCents = 6_000) {
|
||||
return {
|
||||
id: "pos-1",
|
||||
assetId: "asset-1",
|
||||
filename: "jubel-eroeffnung.jpg",
|
||||
albumName: "Jubel",
|
||||
purpose: "Print Nutzung",
|
||||
reference: "REF-001" as string | null,
|
||||
priceCents,
|
||||
createdAt: new Date("2026-03-15T10:00:00.000Z"),
|
||||
};
|
||||
}
|
||||
|
||||
function baseRepeatDownload() {
|
||||
return {
|
||||
id: "rdl-1",
|
||||
assetId: "asset-1",
|
||||
usageId: "usage-1",
|
||||
authorizedAt: new Date("2026-03-20T14:00:00.000Z"),
|
||||
repeatDownloadNote: "Zweitverwendung im Web" as string | null,
|
||||
};
|
||||
}
|
||||
|
||||
function baseSnapshot(overrides: Partial<BillingReportSnapshot> = {}): BillingReportSnapshot {
|
||||
return {
|
||||
address: baseAddress(),
|
||||
selectedPositions: [basePosition()],
|
||||
repeatDownloads: [baseRepeatDownload()],
|
||||
netTotalCents: 6_000,
|
||||
reportNumber: 1,
|
||||
invoiceNumber: "NN-2026-03-001",
|
||||
period: {
|
||||
fromUtc: new Date("2026-03-01T00:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-04-01T00:00:00.000Z"),
|
||||
},
|
||||
documentId: "doc-00000000-0000-0000-0000-000000000001",
|
||||
createdAt: new Date("2026-04-02T08:00:00.000Z"),
|
||||
legalSeparationNotice:
|
||||
"Dieser Nutzungsnachweis ist keine Rechnung im Sinne des Umsatzsteuerrechts.",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// --- Fixture builders -------------------------------------------------------
|
||||
|
||||
function oneRowFixture(): BillingReportSnapshot {
|
||||
return baseSnapshot();
|
||||
}
|
||||
|
||||
function sixtyRowsFixture(): BillingReportSnapshot {
|
||||
const positions = Array.from({ length: 60 }, (_, i) => ({
|
||||
...basePosition(500),
|
||||
id: `pos-${i + 1}`,
|
||||
assetId: `asset-${i + 1}`,
|
||||
filename: `bild-${String(i + 1).padStart(3, "0")}.jpg`,
|
||||
reference: `REF-${String(i + 1).padStart(3, "0")}`,
|
||||
}));
|
||||
const netTotal = positions.reduce((sum, p) => sum + p.priceCents, 0);
|
||||
return baseSnapshot({
|
||||
selectedPositions: positions,
|
||||
repeatDownloads: [],
|
||||
netTotalCents: netTotal,
|
||||
invoiceNumber: "NN-2026-03-060",
|
||||
});
|
||||
}
|
||||
|
||||
function longAddressFixture(): BillingReportSnapshot {
|
||||
return baseSnapshot({
|
||||
address: {
|
||||
companyName: "Süddeutsche Verlagsgesellschaft mbH & Co. KG Herausgeber-Bevollmächtigte",
|
||||
contactName: "Dr. Maximilian von und zu Müllendorf-Wiesenbach",
|
||||
addressLine1: "Königsallee 42a, Hinterhof 3. Stock, Raum 312",
|
||||
addressLine2: "Nebengebäude bei der alten Mühle",
|
||||
postalCode: "80331",
|
||||
city: "München",
|
||||
countryCode: "DE",
|
||||
},
|
||||
invoiceNumber: "NN-2026-03-LONG",
|
||||
});
|
||||
}
|
||||
|
||||
function longFieldsFixture(): BillingReportSnapshot {
|
||||
return baseSnapshot({
|
||||
selectedPositions: [
|
||||
{
|
||||
...basePosition(12_500),
|
||||
id: "pos-long-1",
|
||||
filename: "jahreshauptversammlung-eroeffnungsrede-vorstandsvorsitzender-2026.jpg",
|
||||
albumName: "Jahreshauptversammlung 2026 — Rückblick und Ausblick",
|
||||
purpose: "Print-Nutzung in der Sonderbeilage zum Jahresrückblick der Zeitung",
|
||||
reference: "REF-Sonderbeilage-2026-Q1-001",
|
||||
},
|
||||
{
|
||||
...basePosition(8_750),
|
||||
id: "pos-long-2",
|
||||
filename: "pressekonferenz-ministerpräsident-bayern-klimaschutzgesetz.jpg",
|
||||
albumName: "Pressekonferenz Klimaschutz",
|
||||
purpose: "Online-Nutzung im Nachrichtenportal sowie Print-Ausgabe",
|
||||
reference: "REF-PK-Klima-2026-03-15",
|
||||
},
|
||||
],
|
||||
netTotalCents: 21_250,
|
||||
invoiceNumber: "NN-2026-03-LONGFIELDS",
|
||||
});
|
||||
}
|
||||
|
||||
function umlautsFixture(): BillingReportSnapshot {
|
||||
return baseSnapshot({
|
||||
selectedPositions: [
|
||||
{
|
||||
...basePosition(3_500),
|
||||
id: "pos-umlaut-1",
|
||||
filename: "münchen-strße-österreich.jpg",
|
||||
albumName: "Münchner Straße Österreich",
|
||||
purpose: "Äöüß Nutzung für Flüsse und Öfen",
|
||||
reference: "REF-ÄÖÜß-001",
|
||||
},
|
||||
],
|
||||
repeatDownloads: [
|
||||
{
|
||||
...baseRepeatDownload(),
|
||||
id: "rdl-umlaut-1",
|
||||
repeatDownloadNote: "Zweitverwendung für Grüße aus Übersee",
|
||||
},
|
||||
],
|
||||
netTotalCents: 3_500,
|
||||
invoiceNumber: "NN-2026-03-UMLAUT",
|
||||
});
|
||||
}
|
||||
|
||||
function threePageFixture(): BillingReportSnapshot {
|
||||
const positions = Array.from({ length: 45 }, (_, i) => ({
|
||||
...basePosition(300),
|
||||
id: `pos-3p-${i + 1}`,
|
||||
assetId: `asset-3p-${i + 1}`,
|
||||
filename: `seite3-bild-${String(i + 1).padStart(3, "0")}.jpg`,
|
||||
albumName: `Album ${i + 1}`,
|
||||
purpose: `Nutzung ${i + 1}`,
|
||||
reference: `REF-3P-${String(i + 1).padStart(3, "0")}`,
|
||||
}));
|
||||
const downloads = Array.from({ length: 30 }, (_, i) => ({
|
||||
...baseRepeatDownload(),
|
||||
id: `rdl-3p-${i + 1}`,
|
||||
assetId: `asset-3p-${i + 1}`,
|
||||
usageId: `usage-3p-${i + 1}`,
|
||||
repeatDownloadNote: `Hinweis für Download ${i + 1}`,
|
||||
}));
|
||||
const netTotal = positions.reduce((sum, p) => sum + p.priceCents, 0);
|
||||
return baseSnapshot({
|
||||
selectedPositions: positions,
|
||||
repeatDownloads: downloads,
|
||||
netTotalCents: netTotal,
|
||||
invoiceNumber: "NN-2026-03-3PAGE",
|
||||
});
|
||||
}
|
||||
|
||||
// --- Main -------------------------------------------------------------------
|
||||
|
||||
async function main() {
|
||||
await fs.mkdir(OUTPUT_DIR, { recursive: true });
|
||||
|
||||
const fixtures: Array<{ name: string; snapshot: BillingReportSnapshot }> = [
|
||||
{ name: "billing-one-row.pdf", snapshot: oneRowFixture() },
|
||||
{ name: "billing-60-rows.pdf", snapshot: sixtyRowsFixture() },
|
||||
{ name: "billing-long-address.pdf", snapshot: longAddressFixture() },
|
||||
{ name: "billing-long-fields.pdf", snapshot: longFieldsFixture() },
|
||||
{ name: "billing-umlauts.pdf", snapshot: umlautsFixture() },
|
||||
{ name: "billing-stress.pdf", snapshot: threePageFixture() },
|
||||
];
|
||||
|
||||
for (const { name, snapshot } of fixtures) {
|
||||
const result = await renderBillingPdf(snapshot);
|
||||
const outPath = path.join(OUTPUT_DIR, name);
|
||||
await fs.writeFile(outPath, result.bytes);
|
||||
console.log(`${name} ${result.bytes.length} bytes sha256=${result.sha256.slice(0, 16)}…`);
|
||||
}
|
||||
|
||||
console.log(`\nDone. ${fixtures.length} PDFs written to ${OUTPUT_DIR}/`);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user