@haneinfo/document-templates
v0.2.9
Published
Structured instance document templates with PDF and Word generation.
Readme
@haneinfo/document-templates
Structured PDF and Word downloads for readable StateMachine instances, implemented under approved decision 202610-d4f6. See package.json for the current package version.
Install the plugin with definition-registry, form and state-machine. The package name is plural; the manifest id is document-template, matching the definition permission namespace required by the RBAC permission catalog. No Prisma fragment, migration, new capability or plugin API version is needed.
{ "plugin": "@haneinfo/document-templates", "instanceId": "default", "enabled": true,
"required": true, "configRef": "documentTemplates" }runtime: {
documentTemplates: {
pdfFontPath: "/private/fonts/NotoSansTC-Regular.otf",
docxFontName: "Noto Sans TC",
images: { logo: "/private/images/logo.png" },
maxPages: 50, maxTableRows: 500, maxOutputBytes: 10485760,
generationTimeoutMs: 30000, maxConcurrentGenerations: 2,
},
}The strict config rejects unknown properties. pdfFontPath is mandatory and checked at startup;
the font must be a readable OpenType/TrueType file (maximum 32 MiB) with the required CJK glyphs.
Images are registered static PNG/JPEG files, maximum 2 MiB and 4096 pixels in each dimension,
maximum 50 keys. Files are loaded at startup: restarting applies changes. Templates cannot supply
paths, URLs or image data. Supply suitably licensed fonts/images and retain their license notices.
No font ships in the production package. The renamed, SIL OFL 1.1 test subset lives only in
src/__tests__/fixtures, with the full license and source/version/SHA-256 metadata.
See the complete loan receipt and
approval proof definitions. They reference the
equipment-loan App's Form and StateMachine; register logo before adopting the proof.
| HTTP (InteractiveAuthGuard) | Behavior |
| --- | --- |
| GET /state-machine-instances/:id/documents/templates | { items: [{ code, name, version }] } for available latest published templates |
| POST /state-machine-instances/:id/documents | Strict { templateCode, format: "pdf" \| "docx" }; streamed attachment download |
Both routes check the shared state-machine read rule before template availability: submitter, effective instance:read, or matching personal/role readership. Unreadable and missing instances return the same INSTANCE_NOT_FOUND/404. The POST ignores no extra fields. Downloads carry attachment disposition with sanitized ASCII fallback and RFC 5987 UTF-8 filename, nosniff, no-store, correct content type and length. The filename combines template code and application number (instance id when no number exists). Successful generation records DOCUMENT history with actor, template code/version and format, without changing state/revision or sending notifications.
Register registry.register(...documentTemplatesMcpTools.commands({ scopeNamespace })) in the App's
existing registry. Exactly two read tools use <namespace>:instance:read and require a user binding:
list_document_templates({ instanceId }) and
preview_document_text({ instanceId, templateCode }). Names and the resolved preview document
are wrapped in $userContent. Previews do not render files or add history. There is no MCP file
generation or publication tool. The generic registry tools automatically support DocumentTemplate
drafts/patches/validation; people publish via the existing /definitions/publish HTTP route.
Definition permissions are document-template:definition:read, :write, :publish, declared in
the manifest and discovered by RbacPermissionCatalog. Instance readers do not need definition permissions.
PDF uses pdfmake; Word uses docx. Both consume one resolved block tree. Cancellable worker threads bound CPU work, output bytes and concurrent generations. No temporary files are created. Rendering finishes into bounded memory before response headers, then HTTP streams the result, so limit errors return structured responses without partially sent documents. Word page limits use the canonical PDF layout preflight; Word clients may paginate differently when substituting fonts or editing content.
| Error | HTTP |
| --- | --- |
| INSTANCE_NOT_FOUND | 404 |
| TEMPLATE_NOT_AVAILABLE | 409 |
| TEMPLATE_INVALID | 409 (runtime compatibility), 400 (invalid text/render input) |
| SUBJECT_UNAVAILABLE | 500 |
| DOCUMENT_TOO_LARGE | 413 (rows, text, pages or bytes) |
| DOCUMENT_BUSY | 429 |
| DOCUMENT_TIMEOUT | 504 |
| DOCUMENT_ABORTED | 400 (usually the client has already disconnected) |
| DOCUMENT_FAILED | 500 |
| INVALID_INPUT | 400; invalid MCP input returns { ok: false, error } |
| FORBIDDEN | 403; MCP credential has no acting user |
Read agent-guide.md before designing a template. Build with pnpm build, then run
the package tests. Tests require compiled worker output and run with one Vitest worker to avoid
competing cold renderer startup on Windows. All runtime dependencies are pure JavaScript.
Development and maintenance scripts
- Watch compilation during local development:
pnpm --filter @haneinfo/document-templates dev(runstsc -p tsconfig.build.json --watch). - Regenerate the template JSON Schema (
schemas/document-template.spec.json) after modifyingsrc/schema.ts: build first (pnpm --filter @haneinfo/document-templates build), then runnode scripts/schema.mjsinsidepackages/document-templates. - Reproduce the test-only subset font (
src/__tests__/fixtures/HaneinfoFixture.otf, SIL OFL 1.1) with Python andfonttools 4.60.1:python src/__tests__/fixtures/subset-font.py <source-font.otf>insidepackages/document-templates.
Localized Form labels (approved decision 202610-a8d3)
Form field, column and option labels given as zh-TW/en maps are rendered in zh-TW (documents carry no request locale). Template labels and titles are unchanged. Requires plugin API 1.26.0.
