1

I'm using Docxtemplater(v^3.9.1) and jszip(v^)2.6.1 packages to set the data and download template. Below is the content in my issue_uncovered5.docx template. issue_uncovered5.docx

These the requests made when I click on the download button.

router.post("/downloaddoucment", async (request, response, next) => {
  const templatePath = path.resolve(__dirname, 'template', 'issue_uncovered5.docx');
  const content = fs.readFileSync(templatePath, 'binary');
  const zip = new JSZip(content);
  const doc = new Docxtemplater();
  const options = {
    delimiters: {
      start: '<<',
      end: '>>',
    },
    tags: {
      bold: {
        tag: 'w:r',
        children: [
          {
            tag: 'w:rPr',
            children: [
              {
                tag: 'w:b',
              },
            ],
          },
          {
            tag: 'w:t',
            value: node => node,
          },
        ],
      },
      'color-red': {
        tag: 'w:r',
        children: [
          {
            tag: 'w:rPr',
            children: [
              {
                tag: 'w:color',
                attributes: {
                  'w:val': 'FF0000',
                },
              },
            ],
          },
          {
            tag: 'w:t',
            value: node => node,
          },
        ],
      },
    },
  };
  doc.loadZip(zip);
  const docInput = {
    prepared_for: '',
    prepared_by: '',
    month_date: 'Feb 2023',
    ciritcal_issues: [{ issue_id: '4', issue_text: 'This is some redbold' }],
    major_issues: [{ issue_id: '', issue_text: 'No issue found.' }],
    admin_issues: [{ issue_id: '', issue_text: 'No issue found.' }],
  };
  doc.setData(docInput);
  doc.setOptions(options);
  doc.render();
  const output = doc.getZip().generate({ type: 'nodebuffer' });
  const id = request.body.id;
  const outputFilePath = path.resolve(__dirname, 'template', `${id}.docx`);
  await fs.writeFileSync(outputFilePath, output);
  response.status(200).json({ msg: "Doc ready" });
});

router.get("/downloaddoucment", async (request, response, next) => {
  const id = request.query.id;
  const filePath = path.resolve(__dirname, 'template', `${id}.docx`);
  const file = fs.readFileSync(filePath);
  response.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  response.setHeader('Content-Disposition', `attachment; filename="${id}.docx"`);
  response.send(file);
});

when I download the content it shows me "undefined" downloaded doc

I have tried changing the options to below as well

const options = {
  delimiters: {
    start: '<<',
    end: '>>',
  },
  tags: {
    bold: {
      tag: 'b',
      value: node => `<w:r><w:rPr><w:b/></w:rPr><w:t>${node}</w:t></w:r>`,
    },
    'color-red': {
      tag: 'w:rPr',
      value: () => '<w:rPr><w:color w:val="FF0000"/></w:rPr>',
    },
  },
};
   const options = {
    delimiters: {
      start: '<<',
      end: '>>',
    },
    tags: {
      bold: {
        tag: 'w:b',
        value: node => node,
      },
      'color-red': {
        tag: 'w:color',
        attributes: {
          'w:val': 'FF0000',
        },
        value: node => node,
      },
    },
  };

but nothing works. What can I do to make the text formatted(either bold or red or both) in downloaded doc.

Ramz
  • 33
  • 5

0 Answers0