diff --git a/package.json b/package.json index 3b744fe..c0ae0ae 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "type": "module", - "packageManager": "pnpm@11.16.0", + "packageManager": "pnpm@10.34.5", "engines": { "node": ">=24 <25" }, diff --git a/src/modules/pdf/render.test.ts b/src/modules/pdf/render.test.ts index 53dc262..5f03eec 100644 --- a/src/modules/pdf/render.test.ts +++ b/src/modules/pdf/render.test.ts @@ -51,4 +51,11 @@ test("pdf buffer is non-empty and starts with the PDF magic header", async () => const result = await renderBillingPdf(reportFixture); expect(result.bytes.length).toBeGreaterThan(1000); expect(result.bytes.subarray(0, 5).toString("ascii")).toBe("%PDF-"); +}); + +test("same snapshot produces byte-identical deterministic output", async () => { + const a = await renderBillingPdf(reportFixture); + const b = await renderBillingPdf(reportFixture); + expect(a.bytes.equals(b.bytes)).toBe(true); + expect(a.sha256).toBe(b.sha256); }); \ No newline at end of file diff --git a/src/modules/pdf/render.ts b/src/modules/pdf/render.ts index f19881c..88b130d 100644 --- a/src/modules/pdf/render.ts +++ b/src/modules/pdf/render.ts @@ -20,7 +20,7 @@ import { buildDocumentDefinition, FONT_NAME } from "./document"; const FONTS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "fonts"); interface PdfMakeInstance { - createPdf(docDefinition: unknown): { getBuffer(): Promise }; + createPdf(docDefinition: unknown): PdfMakeResult; setFonts(fonts: Record): void; setLocalAccessPolicy(callback: (p: string) => boolean): void; virtualfs: { @@ -29,6 +29,16 @@ interface PdfMakeInstance { }; } +interface PdfMakeResult { + /** Returns the underlying PDFKit document (a Node Readable stream). */ + getStream(): Promise; + getBuffer(): Promise; +} + +interface PdfKitDocument { + _id: Buffer; +} + // pdfmake exports a CommonJS singleton; use createRequire to preserve the // `this` context that methods like setFonts rely on. const require = createRequire(import.meta.url); @@ -70,6 +80,19 @@ export type RenderedPdf = { export async function renderBillingPdf(snapshot: BillingReportSnapshot): Promise { const docDefinition = buildDocumentDefinition(snapshot); const pdf = pdfmake.createPdf(docDefinition); + + // Inject a deterministic PDFKit document /ID derived from the snapshot's + // documentId. PDFKit normally generates a random /ID via MD5 of the info + // dictionary (including a `new Date()` fallback). We override it after + // document creation but before the buffer is flushed so that the same + // snapshot always produces byte-identical output — making the stored + // SHA-256 a verifiable integrity fingerprint rather than a random index. + // We use the first 16 bytes of SHA-256(documentId) to match the 16-byte + // width that PDFKit's MD5-based generateFileID produces. + const docId = createHash("sha256").update(snapshot.documentId).digest().subarray(0, 16); + const stream = await pdf.getStream(); + stream._id = docId; + const bytes = await pdf.getBuffer(); const sha256 = createHash("sha256").update(bytes).digest("hex"); return { bytes, sha256 };