| name | pagedjs |
| description | HTML/CSS print layout design and debugging using Paged.js polyfill library. Use for creating, editing, and troubleshooting paginated documents (books, reports, manuals) with CSS Paged Media features like page breaks, running headers/footers, page counters, and print-specific layouts. Includes browser preview, PDF rendering workflows, and layout debugging. |
Paged.js Print Layout Skill
This skill provides comprehensive support for designing and debugging HTML/CSS layouts using the Paged.js polyfill library for print media. It covers the complete workflow from editing content to reviewing final PDFs.
Workflow Overview
The typical workflow for working with Paged.js documents:
- Edit content - Modify HTML/Markdown source files
- Preview in browser - View with Paged.js polyfill applied
- Debug layouts - Fix page breaks, adjust spacing, refine CSS
- Render to PDF - Generate final output using CLI tools
- Review PDF - Check final output and iterate as needed
When to Use This Skill
Use this skill when working on:
- Multi-page print documents (books, reports, manuals)
- Page layout issues (breaks, spacing, overflow)
- Running headers and footers with dynamic content
- Page numbering and counters
- CSS Paged Media features (@page rules, margin boxes, break properties)
- Converting HTML to print-quality PDFs
- Debugging layout and rendering issues
Core CSS Patterns
Quick Reference
For detailed CSS patterns and examples, see references/css-patterns.md. Key areas covered:
- @page rules - Page size, margins, named pages
- Page breaks - Control where content breaks across pages
- Headers/footers - Running content in margin boxes
- Counters - Page numbers, chapter numbers, figure numbers
- Cross-references - Dynamic page number references
- Layout patterns - Multi-column, bleeds, running elements
When to read css-patterns.md:
- Setting up page structure for first time
- Implementing headers/footers or page numbers
- Need examples of specific CSS features
- Creating complex layouts
Common Patterns Quick Guide
/* Basic page setup */
@page {
size: A4;
margin: 2cm;
}
/* Page breaks */
.chapter {
break-before: page; /* Start new page */
}
h1, h2, h3 {
break-after: avoid; /* Keep with next */
break-inside: avoid; /* Don't split */
}
/* Running headers */
h1 {
string-set: chapter-title content();
}
@page {
@top-center {
content: string(chapter-title);
}
}
/* Page numbers */
@page {
@bottom-center {
content: counter(page);
}
}
Debugging and Troubleshooting
For comprehensive troubleshooting guidance, see references/troubleshooting.md. It covers:
- Page break issues (unwanted, missing, blank pages)
- Header/footer problems (not showing, wrong content)
- Counter issues (not incrementing, wrong values)
- Layout and overflow problems
- Performance issues
- Browser-specific issues
- Diagnostic techniques
When to read troubleshooting.md:
- Encountering specific layout issues
- Headers/footers not behaving as expected
- Page breaks in wrong places
- Content overflowing or positioned incorrectly
- Need systematic debugging approach
Quick Debugging
/* Visual debugging - add temporarily */
* {
outline: 1px solid rgba(255, 0, 0, 0.1);
}
@page {
border: 2px solid red;
}
h1, h2, h3 {
background: rgba(0, 255, 0, 0.1);
}
Scripts
validate_pagedjs.py
Validates HTML and CSS for common Paged.js issues.
Usage:
python scripts/validate_pagedjs.py document.html
python scripts/validate_pagedjs.py document.html --css styles.css
What it checks:
- Paged.js script inclusion
- @page rules and page configuration
- string-set/string() cross-references
- counter-reset/counter-increment usage
- Named pages
- Common CSS issues (absolute positioning, missing break properties)
- Orphans and widows configuration
When to use:
- Before rendering to PDF
- After making significant CSS changes
- Debugging mysterious layout issues
- Checking for missing dependencies
preview_template.py
Generates a minimal HTML preview template for quick CSS testing.
Usage:
python scripts/preview_template.py --output preview.html
python scripts/preview_template.py --css custom.css --output preview.html
When to use:
- Testing new CSS rules quickly
- Isolating CSS issues from complex content
- Creating minimal reproducible examples
- Learning Paged.js features
Working with Page Layouts
Starting a New Document
Set up basic @page rules
@page { size: A4 portrait; /* or Letter, B5, etc. */ margin: 2.5cm 2cm 2.5cm 2cm; }Define page break behavior
/* Chapters start on new page */ .chapter { break-before: page; } /* Keep headings with content */ h1, h2, h3 { break-after: avoid; break-inside: avoid; }Add basic headers/footers
@page { @top-center { content: string(chapter-title); } @bottom-center { content: counter(page); } } h1 { string-set: chapter-title content(); }
Refining Layouts
For page break issues:
- Use browser DevTools to inspect where breaks occur
- Add
break-inside: avoidto elements that split awkwardly - Use
break-after: avoidto keep related content together - Consider
orphansandwidowsproperties for paragraphs
For header/footer issues:
- Verify
string-setelements exist on each page - Check that string names match between set and use
- Use
:firstpseudo-class to hide headers on chapter starts - Use
:leftand:rightfor different recto/verso headers
For spacing issues:
- Reset margins on all elements, then add back systematically
- Use consistent vertical rhythm (e.g., 1em = 1 line)
- Be careful with margin collapse
- Test with actual content length, not just samples
Reviewing PDF Output
After rendering to PDF, check for:
- Page breaks - Verify chapters start correctly, no awkward splits
- Headers/footers - Confirm content updates properly across pages
- Page numbers - Check sequence, formatting, special sections
- Margins - Ensure consistent spacing, no content in bleed areas
- Fonts - Verify all fonts render correctly and are embedded
- Images - Check quality, positioning, and page boundary behavior
- Overall flow - Read through to catch visual rhythm issues
Common Workflow Patterns
Pattern 1: Fixing Page Break Issues
- Identify the problem area (element breaking across pages)
- Add visual debugging CSS to see page boundaries
- Apply
break-inside: avoidto the element - If still breaking, check parent containers
- Consider reducing font size or padding if content is too large
- Validate with
validate_pagedjs.py - Re-render PDF and review
Pattern 2: Setting Up Running Headers
- Identify what content should appear in headers (chapter titles, section names)
- Add
string-setto the element that holds this content - Define
@pagemargin box withcontent: string(...) - Test that content updates across pages
- Add
:firstrules to remove header on chapter starts if desired - Configure different left/right pages if needed
Pattern 3: Debugging Layout Issues
- Run
validate_pagedjs.pyto catch obvious issues - Add visual debugging CSS (outlines, backgrounds)
- Use browser DevTools to inspect computed styles
- Test with simplified content to isolate the issue
- Check
references/troubleshooting.mdfor specific issue patterns - Make incremental changes and test after each one
Pattern 4: Perfecting Final Layout
- Generate full PDF and review page by page
- Note all issues (breaks, spacing, alignment)
- Prioritize issues (critical vs. nice-to-have)
- Fix critical issues first (content loss, illegibility)
- Iterate on refinements (spacing, widows/orphans)
- Do final read-through checking for:
- Consistent vertical rhythm
- Proper heading hierarchy
- Clean page breaks
- Correct headers/footers
- Accurate page numbers
Tips for Large Documents (200+ pages)
- Use named pages for different sections (frontmatter, chapters, appendices)
- Test in sections rather than always rendering entire document
- Optimize images before including (compress, resize)
- Simplify CSS selectors for better performance
- Use browser preview for quick iteration, PDF for final review
- Keep reference documents open (css-patterns.md, troubleshooting.md)
- Validate frequently with
validate_pagedjs.pyto catch issues early - Use version control to track CSS changes and revert if needed
Best Practices
- Start simple - Basic @page rules first, then add complexity
- Test incrementally - Add one feature at a time
- Use semantic HTML - Makes CSS targeting easier
- Keep CSS organized - Group by feature (page setup, breaks, headers, etc.)
- Document custom rules - Add comments for complex CSS
- Preview frequently - Catch issues early before they compound
- Validate before final render - Save time by catching errors early
- Keep backups - CSS changes can have cascading effects
Additional Resources
The skill includes these reference documents:
- css-patterns.md - Comprehensive CSS examples for all Paged.js features
- troubleshooting.md - Systematic debugging guide for common issues
Read these references when you need detailed examples or are troubleshooting specific issues. The patterns and solutions in these documents are proven to work and cover edge cases.