我正在尝试将多个大base64
图像保存到一个pdf
文件中
exportPdfv3p3(imgPath:string, x:number, y:number, width:number, height:number)
{
var pdfFile = this.pdfItem;
var localNumImagesLoaded = this.numImagesLoaded + 1;
var totalNumImages = this.numImages;
var img = new Image;
img.src = imgPath;
var pdfWidth = pdfFile.internal.pageSize.getWidth();
img.onload = function()
{
pdfFile.addImage(img, 'PNG', pdfWidth - width - 5, y, width, height);
console.log("localNumImagesLoaded",localNumImagesLoaded);
if (localNumImagesLoaded > totalNumImages)
alert("numImagesLoaded desync (too many images)");
else if (localNumImagesLoaded < totalNumImages){}
else // (localNumImagesLoaded == numImages)
{
// Total page number plugin only available in jspdf v1.0+
if (typeof pdfFile.putTotalPages === 'function')
{
pdfFile.putTotalPages(2);
}
// pdfFile.save("test.pdf");
pdfFile.output('datauri');
}
};
++this.numImagesLoaded;
console.log("this.numImagesLoaded", this.numImagesLoaded);
}
但是,img.onLoad
似乎是async
,导致pdfFile.output
过早运行并将空白输出到pdf中。
我怎么知道何时实际加载图像并将其添加到pdf,以便我可以在正确的时间保存pdf文件?