Pdf.js (for node) not rendering entire contents of pdf

好久不见. 提交于 2019-12-12 04:36:47

问题


I am trying to search the text of of a pdf using https://www.npmjs.com/package/pdfjs-dist-for-node.

My code looks like this:

 gettext: function(){
     var data = '../static/example.pdf';
         return pdfjs.getDocument(data).then(function(pdf) {
     var pages = [];
     for (var i = 0; i < pdf.numPages; i++) {
                 pages.push(i);
     }
     return Promise.all(pages.map(function(pageNumber) {
                 return pdf.getPage(pageNumber + 1).then(function(page) {
         return page.getTextContent().then(function(textContent) {
                         return textContent.items.map(function(item) {
             return item.str;
                         }).join(' ');
         });
                 });
     })).then(function(pages) {
         return pages.join("\r\n")
     });
         }).then(function(pages){
     console.log(pages)
     });


 }

This seems to work, but it skips parts of the text. Specifically, it skips whatever I can't highlight with the mouse in the original pdf doc. Is there a way to get pdf.js to pick up on this data?


回答1:


If the text is not selectable when you view the PDF, that means it's actually an image, which therefore means you won't be able to search for it.

So unfortunately, this is not possible, unless you set up something else to do some OCR on the PDF first to try to convert images to text.



来源:https://stackoverflow.com/questions/43868315/pdf-js-for-node-not-rendering-entire-contents-of-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!