1. Products
  2.   Word Processing
  3.   JavaScript
  4.   Docx-templates
 
  

JavaScript API to Generate Dynamic Word Documents

Open Source JavaScript Library for Creating Dynamic Word Documents with Node.js and Browser-based pplications.

What is Docx-templates?

Docx-Templates is an open-source JavaScript library that enables software developers to generate dynamic Microsoft Word (.docx) documents using predefined templates. Instead of manually building Word documents programmatically, developers can design templates in Microsoft Word and inject dynamic data using embedded commands. This library works in both Node.js and browser environments, making it highly versatile for modern web and server-side applications. It processes DOCX files by parsing their internal XML structure and replacing template commands with actual data at runtime.

The Docx-Templates library is a powerful, open-source tool designed for both Node.js and browser environments. The primary advantage of the library is the separation of design and logic. Non-technical users can design the layout and styling in Word, while developers focus on injecting the data. It supports complex logic like loops, conditional formatting, and even the execution of JavaScript snippets directly within the document. This makes it an ideal solution for developers who need to produce high-fidelity reports without wrestling with XML or proprietary design tools.

Previous Next

Getting Started with Docx-templates

The recommended way to install Docx-templates library is via npm. Please use the following command for a smooth installation

Install Docx-Templates via NPM

$ npm install docx-templates 

Install Docx-Templates via Yarn/strong>

$ yarn add docx-templates 

Template-Based Document Generation via JS API

The open source Docx-Templates library allows software developers to design documents in Word and inject data dynamically using simple commands. This approach separates presentation from logic, making templates easy to manage and update without changing code. You can directly access properties from your data object or even run simple expressions. For example, to insert a full name, your template would look like this: {firstName} {surname}. The library evaluates the code and replaces it seamlessly.

How to Dynamically Create Word Documents inside JavaScript Apps?

import createReport from 'docx-templates';
import fs from 'fs';

const template = fs.readFileSync('template.docx');

const report = await createReport({
  template,
  data: { name: 'John', surname: 'Doe' },
});

fs.writeFileSync('output.docx', report);

Dynamic Image Injection via JavaScript

Beyond text, the open source Docx-Templates library can inject images on the fly. This includes local files, images from a URL, or even programmatically generated QR codes. You can control the size of the image and its placement. This feature is a game-changer for creating identification badges, product catalogs, or reports that require dynamic charts and visual evidence. Here is a simple example that demonstrates how to insert image into a Word documents via JavaScript API.

How to Insert Image on the Fly inside Word Documents via JavaScript API?

const buffer = await createReport({
  template,
  data: {
    userAvatar: async () => {
      const resp = await fetch('https://example.com/photo.png');
      return { width: 3, height: 3, data: await resp.arrayBuffer(), extension: '.png' };
    }
  },
});
// In Word: {IMAGE userAvatar()}

Powerful Loops (FOR/END-FOR)

Handling lists of data is seamless with the FOR command. This feature allows you to iterate over arrays to generate table rows, lists, or even entire sections of a document. The Docx-Templates library has included support for nested loops, allowing you to represent complex hierarchical data structures, such as a list of departments with their respective employees, all while maintaining the original Word styling. The following code example shows how to achieve this.

How to Apply Advanced Loop inside JavaScript Apps?

const buffer = await createReport({
  template,
  data: {
    projects: [
      { name: 'SEO Optimization', status: 'Completed' },
      { name: 'API Integration', status: 'In Progress' }
    ]
  },
});
/* In Word:
{FOR project IN projects}
Project: {project.name} - Status: {project.status}
{END-FOR}
*/

Custom JavaScript Context (EXEC)

One of the most advanced features of the Docx-Templates library is the ability to run arbitrary JavaScript snippets using the EXEC (or !) command. By providing an additionalJsContext, you can expose custom helper functions to the template. This allows the template to perform calculations, format dates, or manipulate strings internally without needing to pre-process every piece of data in your main application logic.