What is docxtemplater?
Docxtemplater is an npm package that allows you to generate and modify Word documents (DOCX) by using templates. It is particularly useful for creating documents with dynamic content, such as invoices, reports, or any document that requires data to be inserted programmatically.
What are docxtemplater's main functionalities?
Template Rendering
This feature allows you to render a DOCX template by replacing placeholders with actual data. In this example, a template file 'template.docx' is read, and placeholders for 'firstName' and 'lastName' are replaced with 'John' and 'Doe', respectively. The modified document is then saved as 'output.docx'.
const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');
const content = fs.readFileSync('template.docx', 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip);
doc.setData({
firstName: 'John',
lastName: 'Doe'
});
doc.render();
const buf = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('output.docx', buf);
Conditional Content
Docxtemplater supports conditional content, allowing sections of a document to be included or excluded based on data. In this example, the 'showSection' variable controls whether a section of the document is displayed.
const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');
const content = fs.readFileSync('template.docx', 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip);
doc.setData({
showSection: true
});
doc.render();
const buf = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('output.docx', buf);
Looping Over Data
This feature allows you to loop over arrays of data to generate repeated sections in a document. In this example, an array of items is used to populate a list in the document, with each item's name and price being inserted.
const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');
const content = fs.readFileSync('template.docx', 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip);
doc.setData({
items: [
{ name: 'Item 1', price: 10 },
{ name: 'Item 2', price: 20 }
]
});
doc.render();
const buf = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('output.docx', buf);
Other packages similar to docxtemplater
handlebars
Handlebars is a popular templating engine that can be used for generating HTML, but it can also be adapted for other text-based formats. Unlike Docxtemplater, Handlebars does not natively support DOCX files, so additional processing would be required to integrate it with Word documents.
mustache
Mustache is another logic-less templating engine similar to Handlebars. It is used for rendering templates in various formats, but like Handlebars, it does not directly support DOCX files. It is more general-purpose compared to Docxtemplater, which is specifically designed for Word documents.
jsreport
jsreport is a reporting platform that can generate various document formats, including DOCX. It provides more comprehensive reporting features compared to Docxtemplater, such as scheduling and web-based editing, but it is also more complex to set up and use.
docxtemplater

docxtemplater is a library to generate docx/pptx documents from a docx/pptx template. It can replace {placeholders} with data and also supports loops and conditions. The templates can be edited by non-programmers, for example your clients.
docxtemplater is very robust because of the many fixed issues over the years, and the high quality of tests and code.
Features
Demo Site
Quickstart
Documentation
The full documentation of the latest version can be found here.
See CHANGELOG.md for information about how to migrate from older versions.
Modules
Functionality can be added with the following paid modules :
- Image module to add a given image with the syntax:
{%image}
; - Html Module to insert formatted text in a docx document with the syntax
{~html}
; - XLSX Module to be able to do templating on Excel files (xlsx extension), also with loops and conditions;
- Chart Module to replace a chart by using data from the JSON object that you give with the syntax
{$chart}
; - Html-Pptx Module to insert formatted text in a pptx document with the syntax
{~html}
; - Error Location Module to show the errors in the template using Word comments
- Slides Module to create multiple slides dynamically with the syntax
{:users}
; - Subtemplate Module to include an external docx file inside a given docx file with the syntax
{:include doc}
; - Subsection Module to include subsections (headers/footers) from an other document with the syntax
{:subsection doc}
; - Subtemplate-pptx Module to include an external pptx file inside a given pptx file with the syntax
{:include doc}
; - Word-Run Module to include raw runs (<w:r>) inside the document with the syntax
{r@wrun}
. This makes it possible to include styled text without having to remove the enclosing paragraph like in the {@rawXml} tag; - QrCode Module to replace an image, keeping any existing properties;
- Table Module to create tables from two dimensional data using the syntax
{:table data}
; - Meta Module to make a document readonly, add a text watermark or update the margins;
- Styling Module restyle a paragraph, a cell or a table depending on some data using the syntax
{:stylepar style}
; - Footnotes Module to be able to add footnotes to a document using the syntax
{:footnotes foot}
- Paragraph Placeholder Module to simplify conditions that should show or hide a given paragraph using the syntax
{?tag}
About docxtemplater
Docxtemplater is my main job, and has been maintained for over 8 years. Expect to get great support if you buy any modules, and also good support on the open-source version.