Frequently asked questions

Inserting new lines

pre = '<w:p><w:r><w:t>';
post = '</w:t></w:r></w:p>';
lineBreak = '<w:br/>';
text = pre + 'testing line 1' + lineBreak + 'testing line 2' + post;
data = {text : text}

then in your template, just put {@text} instead of the usual {text}

If you use the angular-parser, you can also write a filter like this:

angularexpressions.filters.raw = function (text) {
    var lines = text.split("\n");
    var pre = "<w:p><w:r><w:t>";
    var post = "</w:t></w:r></w:p>";
    var lineBreak = "<w:br/>";
    return pre + lines.join(lineBreak) + post;
data = {text: "testing line 1 \n testing line 2"};

and then have your docx as : {@text|raw}

Insert HTML formatted text

It is possible to insert HTML formatted text using the HTML pro module

Generate smaller docx using compression

The size of the docx output can be big, in the case where you generate the zip the following way:

docx.getZip().generate({ type: "nodebuffer"})

This is because the zip will not be compressed in that case. To force the compression (which could be slow because it is running in JS for files bigger than 10 MB)

var zip = docx.getZip().generate({
        type: "nodebuffer",
        compression: "DEFLATE"

Writing if else

To write if/else, see the documentation on sections for if and inverted sections for else.

You can also have conditions with operators > and < using angular parser conditions.

Conditional Formatting

With the PRO styling module it is possible to have a table cell be styled depending on a given condition (for example).

Using data filters

You might want to be able to show data a bit differently for each template. For this, you can use the angular parser and the filters functionality.

For example, if a user wants to put something in uppercase, you could write in your template :

{ | uppercase }

See angular parser for comprehensive documentation


Docxtemplater is quite fast, for a pretty complex 50 page document, it can generate 250 output of those documents in 44 seconds, which is about 180ms per document.

There is also an interesting blog article at that explains how I optimized loops in docxtemplater.

Support for IE9 and lower

docxtemplater should work on almost all browsers as of version 1 : IE7 + . Safari, Chrome, Opera, Firefox.

The only ‘problem’ is to load the binary file into the browser. This is not in docxtemplater’s scope, but here is the code that jszip’s creator recommends to use to load the zip from the browser:

The following code should load the binary content on all browsers:

JSZipUtils.getBinaryContent('path/to/', function(err, data) {
  if(err) {
    throw err; // or handle err

  var zip = new JSZip(data);

Get list of placeholders

To be able to construct a form dynamically or to validate the document beforehand, it can be useful to get access to all placeholders defined in a given template. Before rendering a document, docxtemplater parses the Word document into a compiled form. In this compiled form, the document is stored in an AST which contains all the necessary information to get the list of the variables and list them in a JSON object.

With the simple inspection module, it is possible to get this compiled form and show the list of tags. suite :

var InspectModule = require("docxtemplater/js/inspect-module");
var iModule = InspectModule();
doc.render(); // doc.compile can also be used to avoid having runtime errors
var tags = iModule.getAllTags();

With the following template :



It will log this object :

    "company": {},
    "users": {
        "name": {},
        "age": {},

The code of the inspect-module is very simple, and can be found here :

Convert to PDF

It is not possible to convert docx to PDF with docxtemplater, because docxtemplater is a templating engine and doesn’t know how to render a given document. There are many tools to do this conversion.

The first one is to use libreoffice headless, which permits you to generate a PDF from a docx document :

You just have to run :

libreoffice --headless --convert-to pdf --outdir . input.docx

This will convert the input.docx file into input.pdf file.

The rendering is not 100% perfect, since it uses libreoffice and not microsoft word. If you just want to render some preview of a docx, I think this is a possible choice. You can do it from within your application by executing a process, it is not the most beautiful solution but it works.

If you want something that does the rendering better, I think you should use some specialized software. PDFtron is one of them, I haven’t used it myself, but I know that some of the users of docxtemplater use it. (I’m not affiliated to PDFtron in any way).

Pptx support

Docxtemplater handles pptx files without any special configuration (since version 3.0.4).

It does so by detecting whether there is a file called “/word/document.xml”, if there is one, the file is “docx”, if not, it is pptx.