Document Template System
The Document Template System enables attorneys to create reusable Word document templates with variable placeholders, generate personalized documents for clients, and deliver professional, formatted DOCX files.
Overview
Key Features
- ✅ Simple Template Creation - Use Word with
syntax - ✅ Automatic Variable Extraction - System detects all template variables
- ✅ Dual Rendering - HTML preview + downloadable DOCX
- ✅ Format Preservation - All Word formatting maintained in output
- ✅ Real-Time Updates - Edit variables and regenerate instantly
- ✅ Pre-filled Values - Client data auto-populated
Architecture
Template Upload Flow
Document Generation Flow
Technologies
Handlebars.js
HTML rendering for browser preview
- Custom
syntax - Helpers for date formatting, currency, etc.
- Fast rendering (~50-100ms)
docxtemplater
DOCX generation with formatting preservation
- Configured with double-brace delimiters
- Maintains all Word styles, fonts, tables
- ~200-500ms per document
NuxtHub Blob (R2)
DOCX file storage
- Original templates
- Generated documents
- Fast CDN delivery
Data Model
Template Storage
typescript
interface DocumentTemplate {
id: string
name: string
description: string
category: string
// Original DOCX in R2
docxBlobKey: string
// Parsed HTML for preview
content: string
// Extracted variables
variables: string[] // ["clientName", "fee", ...]
requiresNotary: boolean
createdAt: number
updatedAt: number
}Generated Document
typescript
interface Document {
id: string
title: string
status: 'DRAFT' | 'SENT' | 'VIEWED' | 'SIGNED' | 'COMPLETED'
templateId: string
matterId: string
clientId: string
// Rendered HTML
content: string
// Generated DOCX in R2
docxBlobKey: string
// Filled variable values
variableValues: Record<string, string>
// Timestamps
createdAt: number
updatedAt: number
sentAt?: number
viewedAt?: number
signedAt?: number
}Variable System
Supported Syntax
Only {{variableName}} is supported:
{{clientName}}
{{lead_attorney}}
{{fee}}
{{currentDate}}❌ NOT Supported: Nested notation like {{person.name}}
Pre-filled Variables
When generating a document, the system auto-fills:
| Variable | Source | Example |
|---|---|---|
{{clientName}} | Database | "John Doe" |
{{clientAddress}} | Database | "123 Main St" |
{{currentDate}} | Auto-generated | "January 5, 2026" |
{{fee}} | Placeholder | "$[To be determined]" |
Custom Variables
Any variables in your template that aren't pre-filled appear in the form for manual entry.
Performance
| Operation | Time | Notes |
|---|---|---|
| Template upload | <1s | Parse + store |
| Document generation | <1s | HTML + DOCX |
| Variable updates | <1s | Re-render both formats |
| DOCX download | <500ms | Fetch from R2 |
Workflow
1. Create Template (Attorney)
- Design document in Word
- Add
where needed - Upload to system
- Review extracted variables
2. Generate Document (Attorney)
- Select template
- Choose client
- Optional: Link to matter
- System auto-fills client data
- Fill remaining variables
- Preview in browser
3. Finalize & Deliver
- Update any variables as needed
- Change status to SENT
- Download DOCX for client
- Client signs (optional)
- Mark as SIGNED/COMPLETED
Security
- ✅ Role-Based Access - Only attorneys can upload templates
- ✅ Authorization Checks - Clients see only their documents
- ✅ File Validation - DOCX type and size limits
- ✅ Secure Storage - R2 with access control
Future Enhancements
- Template Versioning - Track changes over time
- Conditional Sections - Show/hide based on data
- Repeating Blocks - Loop over lists (beneficiaries, etc.)
- E-Signature Integration - DocuSign, HelloSign
- Bulk Generation - Create multiple documents at once
Learn More
- User Guide - How to use templates
- API Reference - Technical API docs
- DOCX Processing - Low-level details