Files
bamort/backend/pdfrender/PAGINATION_INTEGRATION.md
T
2025-12-19 08:24:32 +01:00

4.9 KiB

Pagination Integration Summary

Problem

The pagination system was fully implemented but not being used during PDF rendering. When the user changed the MAX value from 32 to 29 in the template, it had no effect because:

  1. The integration test was passing the full skills list directly to the template
  2. The template was trying to render all skills with {{range .Skills}}
  3. The pagination logic was never invoked

Solution

Integrated the pagination system into the rendering workflow:

1. Created Pagination Helper (pagination_helper.go)

  • PreparePaginatedPageData: Prepares data for rendering with proper pagination

    • Takes full view model and template name
    • Returns PageData with lists split according to template capacity
    • Handles all 4 page types (stats, play, spell, equipment)
  • SplitSkillsForColumns: Utility function to split skills into two columns

    • Takes skills list and column capacities
    • Returns (column1Skills, column2Skills)
    • Properly truncates if total exceeds capacity

2. Updated Template Structure

Modified: page1_stats.html

  • Changed {{range .Skills}} to {{range .SkillsColumn1}} for first column
  • Changed empty iteration to {{range .SkillsColumn2}} for second column
  • Now properly renders skills in two separate columns

3. Updated Data Model

Modified: viewmodel.go

  • Added SkillsColumn1 []SkillViewModel to PageData struct
  • Added SkillsColumn2 []SkillViewModel to PageData struct
  • Keeps original Skills field for backward compatibility

4. Updated Template Metadata

Modified: template_metadata.go

  • Changed MaxItems from 32 to 29 for both skill columns
  • Now matches the template comment <!-- MAX: 29 -->
  • Ensures pagination uses correct capacity

5. Fixed All Tests

Updated tests to use the new pagination workflow:

Integration Tests:

  • TestIntegration_FullPDFGeneration: Now uses PreparePaginatedPageData
  • TestIntegration_PaginationWithPDF: Uses SkillsColumn1/Column2
  • TestIntegration_TemplateMetadata: Expects MAX=29
  • TestVisualInspection_AllPages: Uses helper for all 4 pages

Pagination Tests:

  • TestPaginateSkills_MultiColumn: Expects 29+11 instead of 32+8
  • TestPaginateSkills_MultiPage: Expects 58/42 split instead of 64/36
  • TestCalculatePagesNeeded: Updated 64→58 capacity test case

Template Tests:

  • TestRenderTemplate_WithSkills: Now uses SkillsColumn1/Column2

6. Created Comprehensive Tests

New file: pagination_helper_test.go

  • Tests for all 4 page types
  • Validates capacity limits are enforced
  • Tests column splitting logic with edge cases

Results

Test Coverage

  • All 46 tests passing
  • Added 5 new tests for pagination helper
  • Updated 8 existing tests to use new workflow

Capacity Enforcement

Page1 (Stats):

  • Column 1: MAX 29 skills
  • Column 2: MAX 29 skills
  • Total: 58 skills per page

Page2 (Play):

  • Weapons: MAX 30
  • Skills: Various categories with separate limits

Page3 (Spells):

  • Spells: MAX 24 (12+12)
  • Magic Items: MAX 5

Page4 (Equipment):

  • Equipment: MAX 20

Generated Output

Test output available in /tmp/bamort_pdf_test/:

  • page1_stats.pdf - 528KB
  • page2_play.pdf - 531KB
  • page3_spell.pdf - 531KB
  • page4_equip.pdf - 512KB
  • character_sheet_complete.pdf - 612KB (merged)

Impact

Before

// Old approach - no pagination
pageData := &PageData{
    Skills: viewModel.Skills, // All skills passed directly
}
  • All 32 skills rendered in first column
  • Second column empty
  • Changing MAX in template had no effect

After

// New approach - with pagination
pageData, err := PreparePaginatedPageData(viewModel, "page1_stats.html", 1, "18.12.2025")
// pageData.SkillsColumn1 has 29 skills
// pageData.SkillsColumn2 has 3 skills (32 total - 29 = 3 remaining)
  • Skills properly split: 29 in column 1, 3 in column 2
  • MAX value in metadata controls distribution
  • Pagination system now fully integrated

Files Modified

  1. backend/pdfrender/pagination_helper.go - NEW
  2. backend/pdfrender/pagination_helper_test.go - NEW
  3. backend/pdfrender/viewmodel.go - Added SkillsColumn1/Column2 fields
  4. backend/pdfrender/template_metadata.go - Updated MAX values
  5. backend/templates/Default_A4_Quer/page1_stats.html - Use column-specific data
  6. backend/pdfrender/integration_test.go - Use pagination helper
  7. backend/pdfrender/pagination_test.go - Updated expectations
  8. backend/pdfrender/templates_test.go - Use column-specific data

Next Steps

The pagination system is now fully functional. Future enhancements could include:

  1. Auto-generate multiple pages when data exceeds one page capacity
  2. Add overflow indicators (e.g., "Continued on next page")
  3. Support for different page layouts with varying column counts
  4. Template-driven pagination rules in HTML comments