Generating Word documents dynamically is a common requirement for many applications—whether you're creating contracts, invoices, reports, or certificates. Manually editing these documents every time can be tedious and error-prone.
In this article, I’ll walk you through how to automate this process using Node.js and the powerful docxtemplater library. We’ll build a simple script that takes a Word template with placeholders and fills it in with real data, generating a ready-to-use .docx
file in seconds.
Whether you’re building an internal tool or automating document generation in a web app, this guide will give you everything you need to get started.
Pre-requisite
Before getting started, make sure you have the following:
- Node.js installed
- A
.docx
Word file containing placeholders (we’ll see how to format that below) - The following Node.js dependencies:
fs
(built-in)path
(built-in)pizzip
docxtemplater
npm install pizzip docxtemplater
Prepare your word template !
Create a Word document (xxxxxxxx.docx
) and insert variables where you want the dynamic content. Variables should be wrapped in double curly braces, like so:
Company: {society}
Date: {ape}
Address: {siret}
President: {presidence}
Lieu de travail accepté: {check_lieu_travail}
The Node.js Script
Here is the complete script to load the Word document, inject data, and export the final version:
const fs = require("fs");
const path = require("path");
const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const content = fs.readFileSync(
path.resolve(__dirname, "your_document.docx"),
"binary"
);
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true,
});
// Data to inject
const data = {
society: "Jean Dupont",
ape: "14 avril 2025",
siret: "10 rue des Lilas, 75000 Paris",
presidence: "Jean Dupont",
check_lieu_travail: true ? "✔" : "☐", // renders a checkbox
};
try {
doc.render(data);
} catch (error) {
console.error(JSON.stringify({ error: error }, null, 2));
throw error;
}
const buf = doc.getZip().generate({ type: "nodebuffer" });
fs.writeFileSync(path.resolve(__dirname, "output.docx"), buf);
console.log("Document generated successfully!");
Simulating Checkboxes with Unicode Characters
In some documents, especially forms or contracts, you may want to include checkboxes that reflect a user's choice. While Microsoft Word doesn’t provide native form elements when using docxtemplater
, you can simulate checkboxes using Unicode characters.
In the script, we use a ternary condition to insert either a checked box (✔
) or an unchecked box (☐
) based on the value of a boolean field:
check_lieu_travail: true ? "✔" : "☐",
This simple trick visually mimics checkboxes in your final Word document without needing any additional styling or fonts. You can even use other symbols like:
"☑"
(alternative checked box)"✘"
(cross mark)"🗹"
/"🗷"
(less common box styles)
It’s a clean and portable solution for any document where binary choices are needed.
Output
Once executed, the script will generate a file named output.docx
in the same directory, with all placeholders replaced by actual data.
Conclusion
Automating Word document generation with Node.js can save you time, reduce human error, and streamline repetitive tasks in your workflows. With just a few lines of code, the docxtemplater
and pizzip
libraries let you inject dynamic data into professional-looking .docx
templates—perfect for contracts, reports, letters, and more.
This setup is flexible enough to scale with your needs. You can integrate it into larger applications, wrap it in an API, or run it as a CLI tool. The possibilities are endless.
If you're dealing with documents that follow a consistent structure, automating them is a no-brainer—and now you’ve got the tools to do it.