Error when trying to paste qrcode into docx document

225 views Asked by At

I upload a docx file on the client and use fast-xml-parser to parse a docx file into xml, before that, using jszip, I open the document archive.

const doc = fs.readFileSync(files.uploadFile.path);
const zip = new JSZip();
await zip.loadAsync(doc);
const xml = await zip?.file("word/document.xml")?.async("string");
const options = {
      attributeNamePrefix: "@_",
      attrNodeName: "attr", //default is 'false'
      textNodeName: "#text",
      ignoreAttributes: true,
      ignoreNameSpace: false,
      allowBooleanAttributes: false,
      parseNodeValue: true,
      parseAttributeValue: false,
      trimValues: true,
      cdataTagName: "__cdata", //default is 'false'
      cdataPositionChar: "\\c",
      localeRange: "", //To support non english character in tag/attribute values.
      parseTrueNumberOnly: false
};
let docObj = parser.parse(xml, options, true);
docObj['w:document'] = [...docObj['w:document'], await QRCode.toDataURL('2323'), 50, 330, { width: 50, height: 50 }];

After parsing, I try to insert at the end of the document qrcode (using the qrcode library) and as a result, an error occurs docObj.w:document is not iterable

Structure of the document after parsing

docObj:  {
  'w:document': { 'w:body': { 'w:p': [Array], 'w:sectPr': [Object] } }
}

What am I doing wrong, why w:document is not iterable? How can qrcode be inserted into a document?

I also tried using docxtemplater to read the docx file

const content = fs.readFileSync(files.uploadFile.path, 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
doc.setData('test');
doc.render();
const buf = doc.getZip().generate({type: 'nodebuffer'});
fs.writeFileSync('testDoc.docx', buf);

But the file is saved unchanged, is it possible to somehow make changes in the file without the placeholders?

0

There are 0 answers