You are currently offline, serving cached version

API

Constructor

new Docxtemplater(zip[, options])

This constructor is preferred over the constructor without any arguments. The constructor without arguments will be removed in docxtemplater version 4. When calling the constructor with a zip file as the first argument, the document will be compiled during instantiation, meaning that this will throw an error if some tag is misplaced in your document.

The options parameter allows you to attach some modules, and they will be attached before compilation.

  • zip: a zip instance, coming from pizzip or jszip version 2.

  • options: (default {modules:[]})

    You can use this object to configure docxtemplater. It is possible to configure in the following ways:

    • You can pass options to change custom parser, custom delimiters, etc.

    • You can pass the list of modules that you would like to attach.

      For example:

      const options = {
          modules: [new HTMLModule()],
          delimiters: {
              start: "<",
              end: ">",
          },
      };
      const doc = new Docxtemplater(zip, options);
      doc.render(/* data */);
      

This function returns a new Docxtemplater instance

new Docxtemplater() - deprecated

This function returns a new Docxtemplater instance, it is deprecated, you should now use the constructor with two arguments.

Methods

render(tags)

This function replaces all template variables by their values.

  • tags: Type: Object {tag_name:tag_replacement} Object containing for each tag_name, the replacement for this tag. For example, if you want to replace firstName by David, your Object should be:

    { "firstName": "David" }
    

renderAsync(tags)

This function replaces all template variables by their values.

The difference between render and renderAsync is that renderAsync will return a Promise and can use values that are promises.

  • tags: Type: Object {tag_name:tag_replacement} Object containing for each tag_name, the replacement for this tag. For example, if you want to replace firstName by David, your Object should be:

    doc.renderAsync({ firstName: Promise.resolve("David") });
    

Calling doc.renderAsync(tags) is exactly the same as doc.resolveData(tags).then(function() { doc.render() })

getZip()

This will return you the zip that represents the docx. You can then call .generate on this to generate a buffer, string , ... (see pizzip generate docs)

compile() - deprecated

This function is deprecated, instead use the new constructor with two arguments.

This function parses the template to prepare for the rendering. If your template has some issues in the syntax (for example if your tag is never closed like in : Hello {user), this function will throw an error with extra properties describing the error. This function is called for you in render() if you didn't call it yourself. This function should be called before doing resolveData() if you have some async data.

loadZip(zip) - deprecated

This function is deprecated, instead use the new constructor with two arguments.

You have to pass a zip instance to that method, coming from pizzip or jszip version 2

setOptions() - deprecated

This function is deprecated instead use the new constructor with two arguments.

This function is used to configure the docxtemplater instance by changing the parser, delimiters, etc. (See docxtemplater configuration).

attachModule(module) - deprecated

This function is deprecated, instead use the new constructor with two arguments.

This will attach a module to the docxtemplater instance, which is usually used to add new generation features (possibility to include images, HTML, ...). Pro modules can be bought on docxtemplater.com

This method can be called multiple times, for example:

doc.loadZip(zip)
    .attachModule(new HTMLModule())
    .attachModule(new XlsxModule());

setData(Tags) - deprecated

This function is deprecated, instead use doc.render(Tags)

  • Tags: Type: Object {tag_name:tag_replacement} Object containing for each tag_name, the replacement for this tag. For example, if you want to replace firstName by David, your Object should be:

    { "firstName": "David" }
    

resolveData(Tags) - deprecated

This function is deprecated, instead use doc.renderAsync(Tags)

  • Tags: Type: Object {tag_name:tag_replacement} Object containing for each tag_name, the replacement for this tag. For example, if you want to replace firstName by David, use :

    { "firstName": "David" }
    

The function returns a Promise when all data points have been resolved.

Talk with sales Contact us