pdf.js 承诺的问题 [英] Troubles with pdf.js promises

查看:51
本文介绍了pdf.js 承诺的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Javascript 中实现 pdf 字数统计.我遇到了使用 promise 的 pdf.js.有没有办法等到脚本完成后再返回计数?我知道这与承诺的想法背道而驰,但其他 js pdf 阅读器有时会产生一堆乱码或什么都不返回.在当前形式下,该函数始终返回 0 字数.

I'm trying to implement a pdf word count in Javascript. I came across pdf.js which uses promises. Is there a way to wait till the script is done before returning the count? I know that this goes against the idea of promises, but the other js pdf readers out there either sometimes produce a bunch of gibberish or return nothing. In its current form the function always return a word count of 0.

function countWords(pdfUrl){
var pdf = PDFJS.getDocument(pdfUrl);
var count = 0;
pdf.then(function(pdf) {
     var maxPages = pdf.pdfInfo.numPages;
     for (var j = 1; j <= maxPages; j++) {
        var page = pdf.getPage(j);

        var txt = "";
        page.then(function(page) {
            var textContent = page.getTextContent();
            textContent.then(function(page){

            for(var i=0;i<page.items.length;i++){
                txtadd = page.items[i].str
                txt += txtadd.replace(/[^a-zA-Z0-9:;,.?!-() ]/g,'');
            }
                count = count + txt.split(" ").length;

            })
        })
     }
     return count;
});

}

推荐答案

无法以同步方式处理 Promise.countWords 不能立即返回值,必须等待内部承诺(一个用于文档,多个用于页面和文本上下文)被解析.所以 countWords 必须返回一个 Promise 或接受回调.最好的方法是尝试返回并链接 then() 调用.需要加入解析时使用Promise.all:

Promises cannot be handled in sync manner. The countWords cannot return value immediately and has to wait on inner promises (one for document and multiple for pages and text contexts) to be resolved. So countWords must return a Promise or accept callback. Best way is try to return and chain then() calls. When needed to join resolution use Promise.all:

function countWords(pdfUrl){
var pdf = PDFJS.getDocument(pdfUrl);
return pdf.then(function(pdf) { // calculate total count for document
     var maxPages = pdf.pdfInfo.numPages;
     var countPromises = []; // collecting all page promises
     for (var j = 1; j <= maxPages; j++) {
        var page = pdf.getPage(j);

        var txt = "";
        countPromises.push(page.then(function(page) { // add page promise
            var textContent = page.getTextContent();
            return textContent.then(function(page){ // return content promise

            for(var i=0;i<page.items.length;i++){
                txtadd = page.items[i].str
                txt += txtadd.replace(/[^a-zA-Z0-9:;,.?!-() ]/g,'');
            }
                return txt.split(" ").length; // value for page words

            });
        }));
     }
     // Wait for all pages and sum counts
     return Promise.all(countPromises).then(function (counts) {
       var count = 0;
       counts.forEach(function (c) { count += c; });
       return count;
     });
});
}
// waiting on countWords to finish completion, or error
countWords("https://cdn.mozilla.net/pdfjs/tracemonkey.pdf").then(function (count) {
  alert(count);
}, function (reason) {
  console.error(reason);
});

<script src="https://npmcdn.com/pdfjs-dist/build/pdf.js"></script>

这篇关于pdf.js 承诺的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆