问题描述
我有两个方法名saveAsPdf
和downloadPdf
。这两个方法都包含async-await,可能导致downloadPdf
方法在saveAsPdf
之前运行,这是不必要的。
我尝试使用Promise,但结果是相同的。
pdfBytes;
saveAsPDf() {
this.storage.get("pageCanvasJson").subscribe((pagesCanvas: {}) => {
for (let page in pagesCanvas) {
let url;
const pages = this.pdfDoc.getPages();
if (pagesCanvas[page].objects.length > 0) {
this.canvas.loadFromJSON(pagesCanvas[page],async () => {
url = this.rasterize();
console.log(url);
const pngBytes = await fetch(url).then((res) => res.arrayBuffer());
const pngImage = await this.pdfDoc.embedpng(pngBytes);
const currentPage = pages[+page - 1];
const { width,height } = currentPage.getSize();
const pngDim = pngImage.scale(width / this.size.width);
currentPage.drawImage(pngImage,{
x: 0,y: 0,width: pngDim.width,height: pngDim.height,});
this.pdfBytes = await this.pdfDoc.save();
this.pdfDoc = await PDFDocument.load(this.pdfBytes);
});
}
}
},(err) => console.error(err),() => this.downloadPdf());
}
async downloadPdf() {
console.log("??????????????")
this.pdfBytes = await this.pdfDoc.save();
var blob = new Blob([this.pdfBytes],{
type: "application/pdf",});
FileSaver.saveAs(blob,this.pdfName);
this.clearPdfSrc.emit("clear");
this.pdfSrc = "";
this.canvas.clear();
}
我只想在运行downloadPdf()
方法后才运行saveAsPdf()
方法,以便获得最终的pdfBytes
。我认为代码是不言自明的,因此,如果有人对此有更好的方法,那将是感激的。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)