PDF Templates

PDF generation system for creating documents from templates with mapped data fields. Supports multi-page templates, combined fields, fill-in sessions, and e-signature integration.

Source Files

LayerPath
Schemabackend/src/db/schema.ts
Template Routesbackend/src/routes/admin/pdf-templates.ts
Field Def Routesbackend/src/routes/pdf-data-field-definitions.ts
Combined Field Routesbackend/src/routes/pdf-combined-fields.ts
Export Routesbackend/src/routes/pdf-export.ts
Session Routesbackend/src/routes/pdf-document-sessions.ts
Export Servicebackend/src/services/pdf-export-service.ts
Template Servicebackend/src/services/pdf-template-service.ts
Session Servicebackend/src/services/pdf-session-service.ts
Combined Field Servicebackend/src/services/pdf-combined-field-service.ts
Conversion Jobbackend/src/lib/jobs/processors/pdf-conversion.ts
Admin Pagefrontend/src/pages/admin/PdfTemplatesList.tsx
Export Pagefrontend/src/pages/PdfExport.tsx
Componentsfrontend/src/components/pdf/ (8 files)

Database Tables

TablePurpose
pdfTemplatesMain entity — name, description, isActive; original PDF stored via CAS fileIdFiles
pdfTemplatePagesIndividual pages within a template — rendered page image as CAS fileId, width/height
pdfTemplateFieldsPositioned fields on a page (x, y, width, height, font, linkedDataFieldDefinitionId)
pdfCombinedFieldsComposite fields that merge multiple data field definitions (global, shared across templates)
pdfDataValuesStored field values per project (API data + manual overrides)
pdfDocumentSessionsResumable fill-in sessions — fieldValues jsonb, temporaryFields, optional project link
pdfDataFieldDefinitionsReusable field definitions — types api (Dataverse path), metadata, custom

PDF Generation Pipeline

  1. Template design — admin uploads page backgrounds and positions fields via the template editor
  2. Field mapping — each field is linked to a data source (entity field, combined field, or manual input)
  3. Session creation — user starts a fill-in session, optionally linked to a project
  4. Data population — mapped fields auto-fill from entity data; manual fields are filled by the user
  5. Exportpdf-lib renders the final PDF with all field values overlaid on the template pages
  6. Storage — generated PDF is saved as a file and linked to the source entity

E-Signature Integration

TablePurpose
signatureRequestsOutgoing signature requests linked to a PDF document
signedDocumentsCompleted signed documents returned from the signature provider
signedDocumentsMetadataLinks the original, signed, and certificate Files (three CAS file refs)

Signature workflow integrates with external e-signature services for legally binding document signing.

Other PDF Generation Paths

Not everything PDF-shaped goes through this template system:

  • Site protocols (Buildings) have a dedicated pdf-lib renderer at backend/src/services/site-protocol-pdf.ts (letterhead, attendees/topics/open-points, embedded photos) — I#1988, PR#1994.
  • RENEWA-issued invoices render via the billing module (billing_invoice_pdf_versions), not via PDF templates.

Frontend Components

ComponentPurpose
PdfTemplateEditorVisual editor for positioning fields on template pages
PdfFieldMapperMap template fields to data sources
PdfPreviewLive preview of the generated PDF
PdfSessionFormFill-in form for manual fields during a session
PdfExportDialogExport options and download

Relationships

PdfTemplate 1──* PdfTemplatePages 1──* PdfTemplateFields
PdfTemplate 1──* PdfDocumentSessions
PdfTemplateField *──1 PdfDataFieldDefinition
PdfCombinedField *──* PdfDataFieldDefinitions
Session *──1 Users (author)
Session ──> Files (exported PDF)

Documents | Files | Buildings | Projects | Admin Dashboard | Service Layer Pattern | Database Architecture