Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
paths: - "packages/documents/src/template/" - "packages/documents/src/pdf/" - "apps/erp/app/routes/x+/templates+/" - "apps/erp/app/components/DocumentTemplateEditor/"
Document Template Customizer
A block-based template engine in @carbon/documents drives every document PDF
(invoices, quotes, POs, travelers, etc.) and the tracking-label PDF/ZPL. Users
customize templates per-company in a live editor; templates are resolved against
defaults at render time. Related printing/queue mechanics live in
printing-system.md.
Storage (documentTemplate + documentSection tables)
Created by packages/database/supabase/migrations/20260609143732_document-template.sql
(later migrations only backfill blocks JSONB — no structural changes).
documentTemplate— composite PK("id","companyId"),UNIQUE (companyId, documentType)(one template per doc type per company). Columns:documentType(plainTEXT, not a Postgres enum),formatVersion INTEGER DEFAULT 1,blocks JSONB,theme JSONB,settings JSONB, nullableheaderSectionId/footerSectionId(soft refs todocumentSection, no FK — a deleted section is skipped at render), plus audit cols. Realtime-enabled. RLS gated on the settings module (settings_create/update/delete).documentSection— company-global reusable rich-text sections.placement(body|header|footer, plain TEXT),content JSONB,config JSONB(header: logo + visible company fields; footer: registration lineshowRegistrationLine/registrationNumber, merge-field capable). Editing a built-in section forks it into a row (upsertDocumentSection).
JSON columns are untyped in generated DB types. toDocumentTemplate(row, type)
(template/schema.ts) is the one cast point — callers use it instead of
re-deriving the shape. Expect tsc .blocks does not exist errors until DB types
are regenerated; that's expected, not a bug.
Template model (packages/documents/src/template/)
- schema.ts — zod.
documentTemplateTypeSchemaenum = the 9 supported types:salesInvoice, salesOrder, purchaseOrder, quote, packingSlip, stockTransfer, jobTraveler, issue, trackingLabel(notesalesInvoice/salesOrder, not bareinvoice/order).blockSchemais a discriminated union keyed ontype: built-ins carry onlyid+visible(some addoptions); extension blocks includerichText, keyValue, spacer, shared, field, customField, watermark.documentTemplateSchema=formatVersion + blocks + theme + settings + header/footerSectionId.CURRENT_TEMPLATE_FORMAT_VERSION = 1. - defaults.ts —
BLOCK_META(one entry required per block type),DEFAULT_TEMPLATES(one per doc type),DOCUMENT_CATALOG+getDocumentLabel,resolveTemplate(type, stored)(read-time normalizer: falls back to the default and appends missing built-in blocks hidden so old rows survive new blocks — except a block whoseBLOCK_METAsetsshowWhenAppended, appended visible because it prints nothing until its data exists; today onlyapproval),BUILT_IN_SECTIONS,collectSectionIds. - merge.ts —
{token}merge-field catalog;interpolateContentfills header/footer/richText/shared/terms at render (keyValue/field useinterpolateString). A paragraph whose merge fields ALL resolve empty is dropped (and a list left empty with it), so "Approved by {order.approvedBy}" prints nothing on an unapproved PO; a Rich Text or Shared block whose content drops out entirely is not rendered, heading included.
Editor UI
apps/erp/app/components/DocumentTemplateEditor/ — index.tsx (rails + toolbar
with a live-record preview combobox), context.tsx (state/actions), plus
BlockList, BlockConfig, FontConfig, ThemeConfig, SectionFormModal,
TemplatePreview, MergeFieldMenu, NumberRow, LogoCropper, labelConfigs,
useHeaderConfig, useTemplateConflict.
Routes / services
x+/templates+/$type.tsx— loader resolves template + sections + custom fields- preview entities + terms seed; action validates (
documentTemplateValidator,settings.models.ts) and callsupsertDocumentTemplate.
- preview entities + terms seed; action validates (
x+/templates+/$type.preview.tsx— POST renders the draft layout viaDOCUMENT_PDFS(optionally against a real record viapreviewId;buildPreviewPropsinsettings.service.ts).- File routes
file+/<doc>+/$id[.]pdf.tsx— canonical consume path:getDocumentTemplate→toDocumentTemplate→resolveTemplate→resolveSections(collectSectionIds(...))→ensureFont(...)→renderToStream(<DocPDF template sections />). - Services in
apps/erp/app/modules/settings/settings.service.ts:getDocumentTemplate,getDocumentTemplateConfig,upsertDocumentTemplate,getDocumentSections(ByIds),resolveSections,upsertDocumentSection,getTerms. MES mirrors the read path inapps/mes/app/services/inventory.service.ts.
Render architecture (packages/documents/src/pdf/)
Each doc is a thin driver (<Doc>PDF.tsx) + a per-doc block registry. The driver
resolves the template, builds a data bag + merge vars, then renders
<Template>{visibleBlocks.map(registry[block.type])}</Template>. pdf/blocks/<doc>/
holds types.ts, vars.ts, one component per block, and registry.tsx
(Record<DocumentBlockType, BlockRenderer> — must key every block type; unused
→ () => null). pdf/components/Template.tsx is the <Document><Page> chrome.
pdf/preview-documents.tsx (DOCUMENT_PDFS[type] = { Component, sample }) dispatches
the generic preview.
Adding a block type to the union breaks every registry → add the key to all
registries + a BLOCK_META entry + (if built-in) a DEFAULT_TEMPLATES entry.
Approval block (purchase orders)
The built-in approval block (pdf/blocks/purchaseOrder/ApprovalBlock.tsx)
prints a heading, the approver's signature (max 40 pt high) over a line, and a
caption, and nothing while the order is unapproved. Its options, set in the
editor's ApprovalConfig: heading (absent = DEFAULT_APPROVAL_HEADING,
"Approved By"; an empty string prints none), showSignature (default true)
and content, a rich-text caption with merge fields (empty =
DEFAULT_APPROVAL_CAPTION: {order.approvedBy}, a line break,
{order.approvedDate}). "Approved by … on …" text belongs in this caption,
not in a separate Rich Text block, or the PDF prints it twice. It is last in the default PO layout and
is appended visible to saved templates. Its data is PurchaseOrderData.approval
from getPurchaseOrderApproval(…, { withSignature: true })
(purchasing.service.ts), which reads the file recorded on the approval
(approvalRequest.decisionSignaturePath, private signatures bucket) as a data
URL. Every other registry renders it as null. Spec:
.ai/specs/2026-10-28-approval-signatures.md.
Tracking label specifics
trackingLabel renders via pdf/ProductLabelPDF.tsx (block-driven) and ZPL via
zpl/ProductLabelZPL.tsx (generateProductLabelZPL; emits only visible fields in
block order, skips extension/custom blocks — no ZPL equivalent). The queued
print-job path also loads documentTemplate with documentType="trackingLabel"
so auto-printed labels match the customizer (see
printing-system.md, renderItemBuiltIn). Label size/grid
is NOT in the template — it stays a print-time ?labelSize= choice (Avery presets
in @carbon/utils labelSizes; ZPL needs a labelSize.zpl config).
Terms & Conditions
The built-in terms block carries optional rich-text content; renderers call
resolveTerms(block, data.terms, vars) — the authored content (interpolated) when
present, else the company terms table setting (salesTerms/purchasingTerms),
which routes still pass as the seed/fallback.
ZPL/B&W label logos: resolveLabelLogo (@carbon/documents/labels) crops, thresholds and resizes the company logo with renderLabelLogo (@carbon/files/media) into a mono PNG data URL and a ^GFA field.