How to get pdf title using pdf.js? [duplicate]

馋奶兔 提交于 2019-12-11 10:53:54

问题


The question is: How can I get the name of the pdf file using pdf.js? I'm running a variation of a pdf.js example from node, and I was wondering if it's at all possible to get it. I've been searching through pdf.js's docs/source, but couldn't find anything obvious. I'm using this code, which (so far) shows the number of pages of each file found on a given folder (in this case, the directory this code is being run from):

var fs = require('fs');
var glob = require('glob');

global.window = global;
global.navigator = { userAgent: "node" };
global.PDFJS = {};
global.DOMParser = require('./domparsermock.js').DOMParserMock;

require('../../build/singlefile/build/pdf.combined.js');
glob("**/*.pdf", function (er, files) {
for(var i = 0; i < files.length; i++){
var data = new Uint8Array(fs.readFileSync(files[i]));
PDFJS.getDocument(data).then(function (doc) {
      var numPages = doc.numPages;
      console.log('Number of Pages: ' + numPages);
      console.log();
    }).then(function () {
      console.log('# End of Document');
    }, function (err) {
      console.error('Error: ' + err);
    });
   }
});

I thought the name of the file was in the doc object as an attribute or something like that, but that doesn't seem to be the case here, and I couldn't find anything about this in the docs. Is there something I'm missing or doing wrong here?


回答1:


I fixed it :) the code looks like this now:

var fs = require('fs');
var glob = require('glob');

global.window = global;
global.navigator = { userAgent: "node" };
global.PDFJS = {};
global.DOMParser = require('./domparsermock.js').DOMParserMock;

require('../../build/singlefile/build/pdf.combined.js');
glob("**/*.pdf", function (er, files) {

//this is the essential change: use a forEach() instead of the for loop
files.forEach(function(file){
    var data = new Uint8Array(fs.readFileSync(file));
    PDFJS.getDocument(data)
      .then(function (doc) {
        var numPages = doc.numPages;
        console.log('File name: ' + file + ', Number of Pages: ' + numPages);
        console.log();
      });
  });
});

Hope it helps someone, and thanks for the quick replies :)



来源:https://stackoverflow.com/questions/30945445/how-to-get-pdf-title-using-pdf-js

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