我需要在由 AJAX 响应确定的页面上放置多个 PDF,当所有文档和页面都呈现后,调用 window.print。
每个文档的页面(画布)都被添加到具有“pdfdoc”类的 div 中,以按文档保持页面顺序。
我是 Promises 的新手,由于异步性质,我无法确定何时调用 window.print。有时在所有页面可用之前调用它,即使我在调用它之前检查渲染承诺中最后一个文档的最后一页。下面是代码:
// Called on each iteration of coredatalist
var getDoc = function(count) {
PDFJS.getDocument(srcUrl).then(function(pdf) {
var currentPage = 1;
$('<div class="pdfdoc" id="doc' + count + '"></div>').appendTo('body');
getPage(pdf, currentPage, count);
});
};
// Called for each page in PDF
var getPage = function(pdf, currentPage, count) {
pdf.getPage(currentPage).then(function(page) {
var scale = 2,
viewport = page.getViewport(scale),
canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
renderContext = {
canvasContext: context,
viewport: viewport,
intent: 'print'
};
// Prepare canvas using PDF page dimensions
canvas.setAttribute('class', 'canvaspdf');
canvas.id = 'canvas' + currentPage;
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
page.render(renderContext).then(function(page) {
$('#doc' + count).append(canvas);
if (currentPage < pdf.numPages) {
currentPage++;
getPage(pdf, currentPage, count);
} else {
if (parseInt(count) === coredatalist.length - 1) {
window.print();
}
}
});
});
};
// Iterate through JSON object from AJAX request
for (var j in coredatalist) {
if (coredatalist.hasOwnProperty(j)) {
srcUrl = coredatalist[j].props.downloaduri;
getDoc(j);
}
}
有谁知道在呈现所有文档和页面后如何调用 window.print ?