DocxTemplater icon

DocxTemplater

Generate an Office document from a template

Overview

This node generates Office documents (specifically DOCX files) from templates using the DocxTemplater library. It takes a DOCX template file as input, applies data context to fill placeholders in the template, and outputs a rendered document. This is useful for automating the creation of customized reports, contracts, invoices, or any document that requires dynamic content insertion based on JSON data.

Typical use cases include:

  • Generating personalized letters or certificates by merging user data into a DOCX template.
  • Creating automated reports where data is dynamically inserted into predefined document layouts.
  • Producing batch documents by running the node once per input item, each with its own data context.

Properties

Name Meaning
Mode Hidden property set to "runOnceForEachItem", meaning the node processes each input item individually.

Note: The provided properties JSON only includes the hidden "Mode" property. However, from the source code and description, the node supports additional parameters (not exposed here) such as:

  • Operation: Currently only "Render" operation is supported.
  • Input File Name: The name of the binary input field containing the DOCX template.
  • Data: JSON object used as the context to fill the template.
  • Options: Collection of optional settings including:
    • Extra modules to extend templating capabilities.
    • Output file name.
    • Paragraph loop handling.
    • Output binary field name.
    • Whether to render newlines inside tags.

Output

The node outputs an array with one item per input item processed. Each output item contains:

  • binary: An object with a single key (default "data" or as configured) holding the rendered DOCX file as binary data.
  • json: An empty object {} (no JSON data output).
  • pairedItem: Metadata linking the output to the corresponding input item index.

The binary output represents the fully rendered DOCX document ready for download, storage, or further processing.

Dependencies

  • Requires the DocxTemplater library along with Pizzip for ZIP file manipulation.
  • Supports loading additional DocxTemplater modules if installed in the n8n environment.
  • Uses an internal JEXL-based parser for advanced templating expressions.
  • May connect to external AI tool nodes for custom filters and data resolvers to enhance template rendering.
  • Input DOCX template must be provided as binary data in a specified input field.
  • No explicit external API keys are required, but proper installation of dependencies and modules in the n8n environment is necessary.

Troubleshooting

  • Common issues:

    • Template errors due to invalid placeholders or syntax in the DOCX template.
    • Missing or incorrectly named binary input fields causing failure to load the template.
    • Errors when extra modules are specified but not installed in the environment.
    • JSON context data not matching template placeholders leading to incomplete rendering.
  • Error messages:

    • "Error while rendering": General catch-all error during template rendering; check logs for detailed cause.
    • "TemplateError" with "Multi error": Indicates multiple template parsing errors; the node surfaces the first error message and description for easier debugging.
  • Resolutions:

    • Verify the DOCX template structure and placeholder syntax against DocxTemplater documentation.
    • Ensure the binary input field name matches the actual input data.
    • Install any extra modules you intend to use via the n8n environment before referencing them.
    • Validate JSON context data correctness and completeness.

Links and References

Discussion