1

I'm trying to generate a .docx file with docxtemplater in nodejs. I am also using the angular-expressions library as per their instructions in the docs. The issue is that everytime I try to access a nested object's field, I get undefined in the generated documents. I can't seem to figure out what I am doing wrong ... In the template file I am accessing the field with {field.property}

const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const angularParser = require("docxtemplater/expressions");

const fs = require("fs");
const { getTemplateFilePath, getGeneratedFilePath } = require("./files.utils");

function generateAnnex1(data, outputName) {
  const templatePath = getTemplateFilePath("annex_1");
  const ouputPath = getGeneratedFilePath("annex_1", outputName);

  generateDoc(templatePath, ouputPath, data);
}

function generateDoc(templatePath, outputPath, data) {
  const content = fs.readFileSync(templatePath, "binary");
  const zip = new PizZip(content);

  const doc = new Docxtemplater(zip, {
    parser: angularParser,
  });

  doc.render(data);

  const buf = doc.getZip().generate({
    type: "nodebuffer",
    compression: "DEFLATE",
  });

  fs.writeFileSync(outputPath, buf);
}

Thanks in advance :)

Gabriel
  • 23
  • 2
  • Did you find out how to do it ? If it still does not work, please provide the data that you are using. It should be something like this : const data = { field : { property: "John" }} – edi9999 May 22 '23 at 13:43

0 Answers0