问题
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