docxtemplater icon indicating copy to clipboard operation
docxtemplater copied to clipboard

The ninth picture can not be shown in docx

Open luchaoqun123 opened this issue 5 years ago • 4 comments

Environment

  • Version of docxtemplater : "docxtemplater": "^3.22.3",
  • Used docxtemplater-modules :"docxtemplater-image-module-free": "^1.1.1"
  • Runner : Browser/vue

How to reproduce my problem :

My template is the following : dataFromJson.json.zip template.docx.zip

With the following ts file :

import docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
import ImageModule from 'docxtemplater-image-module-free'

export default class extends Vue {
//something for page render...


buttonActionClickForExportDocument(){
     let path = '/word.docx'
    JSZipUtils.getBinaryContent(path, async function (error, content) {
      if (error) {
        throw error
      }
      let opts = {
        centered: true,
        fileType: 'docx',
        getImage: function (chartId) {
          return base64DataURLToArrayBuffer(chartId)
        },
        getSize: function () {
          return [500, 300]
        }
      }
      let imageModule = new ImageModule(opts)
      let zip = new PizZip(content)
      let doc = new docxtemplater()
      doc.attachModule(imageModule)
      doc.loadZip(zip)

     //the data from zip
      let dataFromJson = require('./components/dataFromJson')
      doc.setData(dataFromJson)
      try {
        doc.render()
      } catch (error) {
        let e = {
          message: error.message,
          name: error.name,
          stack: error.stack,
          properties: error.properties
        }
        throw e
      }

      let out = doc.getZip().generate({
        type: 'blob',
        mimeType:
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
      })
     saveAs(out,'myDoc.docx')
   }
}
export function base64DataURLToArrayBuffer(dataURL) {
  const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/
  if (!base64Regex.test(dataURL)) {
    return false
  }
  const stringBase64 = dataURL.replace(base64Regex, '')
  let binaryString
  if (typeof window !== 'undefined') {
    binaryString = window.atob(stringBase64)
  } else {
    // eslint-disable-next-line node/no-deprecated-api
    binaryString = new Buffer(stringBase64, 'base64').toString('binary')
  }
  const len = binaryString.length
  const bytes = new Uint8Array(len)
  for (let i = 0; i < len; i++) {
    const ascii = binaryString.charCodeAt(i)
    bytes[i] = ascii
  }
  return bytes.buffer
}

!HERE! are Problems :

  • It works to output the myDoc.docx, the data in the docx is expected.It's ok
  • And, myDoc.docx was open in WPS or Microsoft word (Cracked Version),the last picture is shown
  • BUTBUTBUT, myDoc.docx was open in Microsoft word(Offical Version), the last picture can NOT be shown
  • Why is different when open the same myDoc.docx???

Any help will be appreciate !

luchaoqun123 avatar Sep 09 '21 12:09 luchaoqun123

Hello @luchaoqun123 My job and revenue now comes from docxtemplater paid software. This module was not written by me, it doesn't seem maintained any more, the last publish was 3 years ago : https://www.npmjs.com/package/docxtemplater-image-module-free

Many bugs and features have been added to the paid image module and the last update was as recent as 6 days ago : https://docxtemplater.com/modules/image/

I'm sorry but I can't help you on this one.

edi9999 avatar Sep 09 '21 21:09 edi9999

Hello @luchaoqun123 My job and revenue now comes from docxtemplater paid software. This module was not written by me, it doesn't seem maintained any more, the last publish was 3 years ago : https://www.npmjs.com/package/docxtemplater-image-module-free

Many bugs and features have been added to the paid image module and the last update was as recent as 6 days ago : https://docxtemplater.com/modules/image/

I'm sorry but I can't help you on this one.

Any way ,thank for your replay in time. Was it a bug in the expried version of docxtemplater-image-module? If I use the paid image module,can I aviod the problem?

luchaoqun123 avatar Sep 10 '21 01:09 luchaoqun123

@edi9999

luchaoqun123 avatar Sep 10 '21 01:09 luchaoqun123

Yes, the paid module will not have the problem, most corruptions have been fixed now, and if the corruption is still present, I will provide a fix within less than a few days.

edi9999 avatar Sep 10 '21 12:09 edi9999