Communitygithub.com

Canvas Multi-Page PDF Export

Learning materials/reports one-click export to printable PDF: Multi-page Canvas (calligraphy sheets/worksheets/charts/reports) one-click merge into A4 PDF, with automatic pagination and page numbers. Suitable for teachers creating exam sheets, parents printing practice sheets, and developers exporting visual reports.

What is Canvas Multi-Page PDF Export?

This skill, designed as part of the 'Teaching Toolkit Canvas Chain,' enables seamless export of multi-page Canvas content into a single, printable A4 PDF. It automatically handles pagination, adds page numbers (up to 10 pages), and uses jsPDF (loaded via CDN) for client-side PDF generation. The core functions detect page number flags, estimate page counts with overflow limits, and slice sequential or row-based content across pages. It is ideal for teachers, parents, and developers who need to produce professional-quality, ready-to-print PDFs from educational grids, calligraphy sheets, or data visualizations, and includes a self-test script for validation.

Works with~Claude Code~Codex CLI~Cursor
npx skills add shuangying0001-beep/canvas-multipage-pdf

Ask in your favorite AI

Open a new chat with this agent skill pre-loaded.

Documentation

What does Canvas Multi-Page PDF Export do?

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.addImage without first addPage: subsequent content overlaps on the same page.
  • ❌ Using mm dimensions while canvas is in px: jsPDF unit must be 'px' and format must be [1240,1754].
  • ❌ Exceeding 10 pages without a warning: first use estimatePages to check overLimit, 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

  • composePDF requires jsPDF: in browser <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js">; Node npm i jspdf.
  • WeChat Mini Program has no jsPDF: first use wx.canvasToTempFilePath to 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).

Related Skills