0

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?

eugenes
  • 151
  • 7
  • The error that you get `docObj.w:document is not iterable` is because docObj["w:document"] is a javascript object, not an array, and objects are not iterable. But still, even if you fixed this issue, you can't simply add a qrcode like this directly inside word/document.xml. The word/document.xml – edi9999 Aug 06 '21 at 21:44
  • To insert an image into a word doc, one needs to add a `` tag , like described here : https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.drawing?view=openxml-2.8.1 The drawing tag contains an rid attribute, which references the relationship file, word/_rels/document.xml.rels, which contains a list of relationships with a rId and a target. With docxtemplater, inserting images can only be done with a placeholder ({%image}), with a paid module https://docxtemplater.com/modules/image/ (I'm the creator of docxtemplater). – edi9999 Aug 06 '21 at 21:48
  • @edi9999 Thx for answer. Please tell me if there are any other options for inserting img into docx documents? Or maybe it's easier to translate them into pdf and work with it already? – eugenes Aug 09 '21 at 08:59

0 Answers0