Canvas Multi-Page PDF Export
Teaching Toolkit · Utility Module. Pure frontend, jsPDF loaded via CDN.
What This Is (For Humans)
Calligraphy sheets, worksheets, and tables are ready, but printing them as a booklet by saving each image individually is too troublesome. This skill merges multi-page Canvas into a standard A4 PDF with one click — automatic page numbers, automatic pagination, with a hard limit of 10 pages, ready to download and print. Great for teachers creating practice sheets, parents printing calligraphy books, etc.
- Includes
selftest.js: validates page number detection, pagination, and slicing logic; only pass when tests are PASS.
When to Use
- "Export these pages as PDF" / "Generate a printable A4"
- Templates with page numbers (multi-page consecutive numbering)
- Input exceeds a single page and needs automatic pagination
Key Parameters (AI Must Read)
- A4 internal dimensions fixed at 1240×1754 (150dpi); jsPDF
unit:'px', format:[1240,1754]. - Image quality
toDataURL('image/jpeg', 0.95). - Hard limit of 10 pages: triggers a confirmation dialog if exceeded; truncates to 10 pages.
Core Function Signatures (AI Direct Call)
// Page number element detection: whether template supports multi-page page numbers
PdfExport.hasPageNoFlag(elements) // -> true/false
// Pagination capacity: { pages, overLimit }
PdfExport.estimatePages(totalCells, cellsPerPage, maxPages=10)
// Slice per page (assign input to pageNo page)
PdfExport.sliceSequential(input, pageNo, cellsPerPage) // skips \r\n
PdfExport.sliceByRow(input, pageNo, rows)
// Compose multi-page PDF (requires jsPDF environment)
// pages: [{ dataURL }] or canvas array
var doc = PdfExport.composePDF(pages, { pageSize:[1240,1754], quality:0.95 });
doc.save('LearningPaper.pdf'); // triggers browser download
Export Flow
1. hasPageNoFlag → whether page number controls are needed
2. Loop i=1..pageCount: drawAll({pageNo:i}) → canvas.toDataURL('image/jpeg',0.95) → collect
3. composePDF(pages) → doc.save()
4. Single-page / no page number template: keep original PNG download behavior
Anti-Patterns (Avoid)
- ❌ Direct
doc.addImagewithout firstaddPage: subsequent content overlaps on the same page. - ❌ Using mm dimensions while canvas is in px: jsPDF
unitmust be'px'and format must be[1240,1754]. - ❌ Exceeding 10 pages without a warning: first use
estimatePagesto checkoverLimit, then show a confirmation dialog before truncating.
Self-Test
node references/selftest.js # Validates page number detection, pagination, slicing. Pass before delivery.
Referenced Files
references/pdf-export.js— Page number detection + pagination + slicing + composition (UMD)references/selftest.js— Node self-test (pure logic, no jsPDF needed)
Important Notes
composePDFrequires jsPDF: in browser<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js">; Nodenpm i jspdf.- WeChat Mini Program has no jsPDF: first use
wx.canvasToTempFilePathto export each page as an image, then combine PDF in web-view/backend, or reuse this logic to compose the PDF on the front-end page. - Complements
canvas-grid-engine/svg-to-canvas-replica: this skill handles "export PDF," while the other two handle "draw/replicate."
Teaching Toolkit Canvas Chain (Combined Use)
This skill is part of the "Teaching Toolkit · Canvas Chain," with four modules covering the complete loop of "draw → replicate → export → validate":
- Canvas Learning Paper Grid Engine (Draw): 13 educational grids + Chinese character centering + pinyin annotation, generating mi-zi-ge/tian-zi-ge calligraphy sheets.
- SVG-to-Canvas Replication Engine (Replicate): Replicates existing paper template SVGs to Canvas with zero errors, editable and printable.
- Canvas Multi-Page PDF Export (Export): One-click merge of multi-page Canvas into standard A4 PDF for direct printing.
- Mini Program Canvas Validation Workflow (Validate): Local preview + screenshot visual regression to ensure no misaligned grids or shifted characters.
See the "Teaching Toolkit Canvas Chain Landing Page" for the full solution and demo (link to be filled in after publication).