This project generates dynamic .docx documents using Docxtemplater and PizZip in Node.js.
It is designed for long legal documents where preserving the original DOCX formatting (tables, headers, styles, pagination, etc.) is critical.
The input data is injected into a Word template (.docx) and the final rendered document is saved as a new .docx file.
- Uses Docxtemplater for advanced templating logic
- Preserves original DOCX formatting
- Supports:
- Variables
- Conditional logic
- Loops (arrays)
- Nested objects
- Ideal for legal documents, contracts, agreements
- Simple Node.js setup
.
├── sample_doc.docx # Input Word template
├── op.docx # Generated output file
├── server.js # Main Node.js script
├── package.json
└── README.md
- Node.js
- Docxtemplater
- PizZip
- fs (File System)
- path
Make sure you have Node.js installed.
👉 Download from: https://nodejs.org/
- Clone the repository:
git clone https://github.com/DINAKAR-S/Docxtemplater.git
cd Docxtemplater- Install dependencies:
npm install docxtemplater pizzip(optional) when using azure function
npm install docxtemplater pizzip @azure/storage-blob-
Place your Word template file in the project directory (default name:
sample_doc.docx) -
Update the
dataobject insideserver.jswith your required values. -
Run the script:
node ./server.js- The generated document will be created as:
op.docx
Docxtemplater uses various types of tags enclosed in curly braces {} to manage template data. Here are the main tag types:
Simple variable replacement:
Hello {name}!
Data: {"name": "John"} → Output: Hello John!
Iterate over arrays:
{#products}
{name}, {price} €
{/products}
Data: {"products": [{"name": "Windows", "price": 100}]} → Creates multiple rows
Show/hide content based on boolean values:
{#hasKitty}Cat’s name: {kitty}{/hasKitty}
{#hasDog}Dog’s name: {dog}{/hasDog}
Sections handle both loops and conditions based on data type:
| Type of Value | Section Behavior |
|---|---|
| falsy or empty array | Never shown |
| non-empty array | Each element |
| object | Once, with object as scope |
| truthy value | Once, unchanged scope |
Opposite of regular sections (render when value is false/empty):
{^repo}No repos :({/repo}
Create tables with dynamic rows:
| Name | Age | Phone Number |
{#users}{name}|{age}|{phone}{/}
Insert raw XML content:
{@rawXml}
Advanced feature for complex formatting.
The script supports complex nested data like:
- Borrowers
- Guarantors
- Trustees
- Conditional sections
- Repeating sections
Example (simplified):
{
Reference: "Test",
Borrower: "ABCDEFGH Holdings Pty Ltd",
Guarantors: [
{
Guarantors_Description: "John Smith",
Emails: ["john.smith@example.com"]
}
],
is_trustee_present: true
}To avoid unwanted line breaks in loops:
const doc = new Docxtemplater(zip, {
paragraphLoop: true // Removes outer section newlines
});Change delimiter characters if needed:
const doc = new Docxtemplater(zip, {
syntax: {
changeDelimiterPrefix: "$" // Use $ instead of {= =} syntax
}
});- All document logic (loops, conditions, placeholders) is handled inside the DOCX template using Docxtemplater syntax.
- This approach ensures legal document formatting is not broken.
nullGetteris used to avoid template errors when values are missing.
- Legal agreements
- Loan documents
- Contracts
- Offer letters
- Compliance documents
- Any long-form DOCX generation
MIT License Feel free to use, modify, and distribute.
Pull requests are welcome. If you find a bug or want to improve the template logic, feel free to contribute.
Open an issue or reach out if you need help with Docxtemplater logic or complex DOCX templates.