Fix blank pages between TOC and first song #33

Closed
opened 2026-03-17 16:32:38 +01:00 by shahondin1624 · 0 comments

Problem

Empty/blank pages appear between the table of contents and the first song (e.g. pages 9-12 are blank).

Root Cause

Two bugs in TOC page allocation:

  1. TocGenerator.estimateTocPages() uses a rough estimate of 40 entries per A5 page. With ~296 songs this estimates 10 TOC pages, but the actual rendered TOC may only need 8.

  2. PdfBookRenderer blindly adds tocPages - 1 blank pages after the TOC table renders, regardless of how many pages the table already consumed. Since OpenPDF's PdfPTable auto-paginates across multiple pages, the TOC table itself already uses several pages. The padding logic then adds additional blank pages on top, resulting in far more TOC pages than allocated.

Fix

  1. Add measurePages() to TocRenderer that pre-renders the TOC to a temporary document and counts actual pages needed (rounded to even for double-sided printing).
  2. Update SongbookPipeline to use the measured page count instead of the rough estimate, and re-generate TOC entries with corrected page numbers.
  3. Fix PdfBookRenderer padding logic to use writer.pageNumber after TOC rendering, only adding blank pages to reach the allocated count (not blindly adding tocPages - 1).

Acceptance Criteria

  • No blank pages between TOC and first song content
  • TOC page numbers correctly reference actual song pages
  • All existing tests pass
  • Double-sided printing constraint maintained (TOC uses even number of pages)
## Problem Empty/blank pages appear between the table of contents and the first song (e.g. pages 9-12 are blank). ## Root Cause Two bugs in TOC page allocation: 1. **`TocGenerator.estimateTocPages()`** uses a rough estimate of 40 entries per A5 page. With ~296 songs this estimates 10 TOC pages, but the actual rendered TOC may only need 8. 2. **`PdfBookRenderer`** blindly adds `tocPages - 1` blank pages after the TOC table renders, regardless of how many pages the table already consumed. Since OpenPDF's `PdfPTable` auto-paginates across multiple pages, the TOC table itself already uses several pages. The padding logic then adds *additional* blank pages on top, resulting in far more TOC pages than allocated. ## Fix 1. Add `measurePages()` to `TocRenderer` that pre-renders the TOC to a temporary document and counts actual pages needed (rounded to even for double-sided printing). 2. Update `SongbookPipeline` to use the measured page count instead of the rough estimate, and re-generate TOC entries with corrected page numbers. 3. Fix `PdfBookRenderer` padding logic to use `writer.pageNumber` after TOC rendering, only adding blank pages to reach the allocated count (not blindly adding `tocPages - 1`). ## Acceptance Criteria - [ ] No blank pages between TOC and first song content - [ ] TOC page numbers correctly reference actual song pages - [ ] All existing tests pass - [ ] Double-sided printing constraint maintained (TOC uses even number of pages)
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shahondin1624/songbook#33