open-xml-templating/docxtemplater

getSize 返回一个promsie ,word预览正常,但是导出word图片丢失

Closed this issue · 2 comments

opts.getImage = (chartId) => {
return that.base64DataURLToArrayBuffer(chartId);
};

opts.getSize = (img, tagValue, tagName) => {
//return [600,400]
return new Promise(function (resolve, reject) {
var image = new Image();
image.src = tagValue;
image.onload = function () {
console.log([image.width, image.height])
resolve([image.width, image.height]);
};
image.onerror = function (e) {
console.log('img, tagValue, tagName : ', img, tagValue, tagName);
}
});

直接返回return [600,400] 图片就是正常的,使用promise图片就丢失

Environment

  • Version of docxtemplater :
  • Used docxtemplater-modules :
  • Runner : Browser/Node.JS/...

How to reproduce my problem :

My template is the following : (Upload the docx file here inside github, which you have to name template.zip (github doesn't accept the docx extension))

With the following js file :

const fs = require('fs');
const Docxtemplater = require('docxtemplater');

const content = fs
    .readFileSync(__dirname + "/template.zip", "binary");

const zip = new PizZip(content);
const doc = new Docxtemplater(zip)

doc.render({
	( INSERT YOUR DATA HERE )
});

const buf = doc.getZip()
             .generate({type:"nodebuffer"});

fs.writeFileSync(__dirname+"/output.docx",buf);

I would expect it to :

  • return the following template (please upload your generated document and expected document)
  • not fail (include error message)
点击下载

If you're using Promises in the image module, you need to use the renderAsync API, like this :

const fs = require('fs');
const Docxtemplater = require('docxtemplater');

const content = fs
    .readFileSync(__dirname + "/template.zip", "binary");

const zip = new PizZip(content);
const doc = new Docxtemplater(zip)

doc.renderAsync({
	( INSERT YOUR DATA HERE )
}).then(function() {
    const buf = doc.getZip()
             .generate({type:"nodebuffer"});
    fs.writeFileSync(__dirname+"/output.docx",buf);
});