Template-based docx report creation for both Node and the browser
open sourceMITself-hostablefree
docx-templates fills Word (.docx and .docm) templates in Node.js, Deno or the browser. Commands in the document (+++INS name+++, or custom delimiters such as { }) insert values, and FOR/END-FOR and IF/END-IF repeat table rows and show content conditionally, with plain JavaScript expressions; IMAGE, LINK and HTML insert pictures, hyperlinks and HTML, and a QUERY command lets the template declare the data it needs (GraphQL, for example). Because templates run JavaScript, its README warns against running templates uploaded by users on a server. It's MIT-licensed and written in TypeScript.
Problems it solves
- generate Word reports in Node.js where the template can run JavaScript to filter and sort the data
- let a .docx template declare its own GraphQL query for the data it needs
- insert QR codes or charts generated on the fly as images in a Word report from JavaScript
- make a .docx in the user's browser from a template, nothing uploaded to a server
- loop over table rows in a Word template with a free npm package, no paid image module
- list all the commands and errors in a Word template before running it
- fill Word templates from Deno
Features
- INS, EXEC, IMAGE, LINK, HTML, FOR/END-FOR, IF/END-IF, ALIAS and QUERY commands
- JavaScript expressions in templates, run in a Node vm sandbox or one you provide
- loops over table rows, nested loops and filter/sort in JavaScript
- dynamic images, hyperlinks and HTML insertion
- literal XML insertion
- custom command delimiters
- error collection (failFast: false) and template inspection
- .docx and .docm templates; Node, Deno, browser and Web Workers
- TypeScript type definitions
Limitations
- Isn't safe for running templates that users upload: templates run JavaScript, and the default Node vm sandbox isn't a security boundary1
Alternatives
The same job
These do what docx-templates does.
- docxtemplater Powerful Word, PowerPoint and Excel generation
- easy-template-x Generate docx documents from templates, in Node or in the browser
- Carbone Generates PDF, DOCX and other documents from JSON data and Office templates
- docxtpl Use a docx as a jinja2 template
- Sablon Ruby document template processor based on docx templates and Mail Merge fields
- Aspose.Words Create, edit, render and convert Word documents to multiple formats without using external software
