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.
Features
- Replace a {placeholder} by a value
- Use loops: {#users} {name} {/users}
- Use loops in tables to generate columns
- Use conditions (if users.length>3) with angular Parsing
- Insert custom XML {@rawXml} (for formatted text for example)
Quickstart
- Installation in node
- Installation in the browser
- Generate a document in node
- Generate a document in the browser
- Generate a document in React, Angular or Vue
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 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 with comments inside the template;
- 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}
Similar libraries
There are a few similar libraries that work with docx, here’s a list of those I know a bit about:
- docx4j : JAVA, this is probably the biggest docx library out there. There is no built in templating engine, but you can generate your docx yourself programmatically.
- docx : Javascript in the browser, you can create your docx from scratch, but not with template syntax, you need to "code your document" in Javascript.
- redocx : Create Docx document from scratch, using JSX syntax, last commit on December 2018.
- officegen : works only server side for the moment.