diff --git a/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.spec.ts b/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.spec.ts index 028c08b..58108d4 100644 --- a/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.spec.ts +++ b/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.spec.ts @@ -1,5 +1,8 @@ import { buildCsv, buildPdf, buildReceivableRows, buildRows } from './cashbox-export.utils'; +// Reads the page count directly out of the raw PDF bytes instead of pulling in +// a parser dependency. Coupled to pdfkit's current /Pages dict serialization - +// a pdfkit upgrade that reorders/reflows it could require adjusting this regex. function pdfPageCount(buffer: Buffer): number { const match = buffer.toString('latin1').match(/\/Type\s*\/Pages[\s\S]{0,80}?\/Count\s+(\d+)/); if (!match) throw new Error('Could not find page count in PDF buffer'); @@ -344,5 +347,8 @@ describe('buildPdf', () => { expect(buffer.subarray(0, 5).toString('utf-8')).toBe('%PDF-'); expect(buffer.length).toBeGreaterThan(2000); + // Guards against the footer loop reintroducing blank trailing pages: with + // the bug, this dataset produced 12 pages (3x the real content pages). + expect(pdfPageCount(buffer)).toBe(4); }); }); diff --git a/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.ts b/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.ts index 8f1a060..3224f0b 100644 --- a/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.ts +++ b/myteamwallet_backend/src/cashbox-export/cashbox-export.utils.ts @@ -313,6 +313,12 @@ function addFooters(doc: PDFKit.PDFDocument, teamName: string): void { for (let i = range.start; i < range.start + range.count; i++) { doc.switchToPage(i); const footerY = doc.page.height - 25; + // footerY sits inside the reserved bottom margin (below pdfkit's page + // maxY()). Without an explicit `height`, pdfkit's LineWrapper measures + // overflow against the full-page maxY() and calls addPage() here on every + // iteration - silently appending blank trailing pages. Bounding the text + // to its own small box (well over the 8pt single-line height needed) + // keeps the overflow check local and stops that auto-pagination. doc .fontSize(8) .font('Helvetica')