Home  >  Q&A  >  body text

Placeholders within docx template are not replaced

I'm using this library to replace some placeholders with docx templates and generate multiple documents. I'm using neutral novo and vue on the front end, and I've created a method that passes the selected docx file and data to the library for processing. I used a for loop in this way

// putting all the desired data into a specific array
for(let i = 0; i < this.selectedData.length; i++){
  this.dataPlaceholders.push({
   key1: val1,
   key2: val2
  })
}
//call the library to get documents

for(let i; i < this.dataPlaceholders.length; i++){
 this.docxTemplate.process(template, this.dataPlaceholders[i])
}

The data to be passed is merged from both arrays and if I console log them I can see all si are in place. As stated in the documentation, I use square brackets {} to set the placeholders, and the placeholders are named the same as the names of each key in the dataPlaceholders batch. After testing, I noticed that I was able to generate a different document, but the placeholders were not replaced and there would be blank fields in the document.

How can I fix it to make this work correctly?

P粉436410586P粉436410586188 days ago339

reply all(1)I'll reply

  • P粉394812277

    P粉3948122772024-03-31 00:57:29

    After some trying and reading some questions on how to implement asynchronous calls inside a loop, I chose to use the Array.prototype.map function and it worked fine. I've modified the vue method that calls the library as an async method, but I've kept the then block so that I'm able to get the processed document after the library has finished processing.

    this.dataPlaceholders.map( async (data, i) => {
      let filename = data.supplier + '.docx'
      this.docxTemplate.process(template, data).then( (result) => {
        // code to download files here
         let a = document.createElement('a')
         let downloadLink = URL.createObjectURL(result)
         a.href = downloadLink
         a.download = filename
         a.click()
      })
    })
    

    The only thing that needs to be solved is how to revoke the blob url after downloading all the files.

    reply
    0
  • Cancelreply