简体   繁体   English

使用 PDF.js 异步加载多个 pdf 文件时获取总页数

[英]Get page number of total pages while asynchronous loading several pdf files with PDF.js

i´m using pdfjs to load and merge some pdf files.我正在使用 pdfjs 加载和合并一些 pdf 文件。 I know how to show page number of a pdf document, but while merging files, i´d like to show a page number based on total pages of all pdf docs.我知道如何显示 pdf 文档的页码,但是在合并文件时,我想根据所有 pdf 文档的总页数显示页码。 But because it's asynchronous, just using and increment a counter variable is not working.但是因为它是异步的,所以仅仅使用和增加一个计数器变量是行不通的。

var actual_page = 1;

(async function loop() {
for (url_item of urls) {
    var loadingTask = pdfjsLib.getDocument(url_item);
        await loadingTask.promise.then(function (pdf) {
            let i = 1;
            while (i <= pdf.numPages) {
                var pageNumber = i;
                pdf.getPage(pageNumber).then(function (page) {
                    //render pdf and print actual_page++ 
                });
            }
        }
    }
}

First page show 1, second page show 3, and 2 will appear on page 10 and so it goes.第一页显示 1,第二页显示 3,2 将出现在第 10 页,依此类推。

Thanks in advance.提前致谢。

I think you're going to need Promise.all or some equivalent to get the pages from getPage in the correct order.我认为您将需要 Promise.all 或某些等效项才能以正确的顺序从 getPage 获取页面。 That might just make your page numbering thing work.这可能只会使您的页码工作正常。

var pagePromises = [];

while (i <= pdf.numPages) {
    pagePromises.push(pdf.getPage(i));
    i++;
}

Promise.all(pagePromises).then(function (allPages) {
  allPages.forEach(page => {
    // render pdf and print actual_page++
  });
});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM