diff --git a/lib/pdf2img.js b/lib/pdf2img.js index dbd050a..1e42048 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -42,22 +42,19 @@ Pdf2Img.prototype.convert = function (file, callbackreturn) { }, function (input, callback) { - var cmd = 'identify -format %n ' + input; + // identify all pages of the PDF file separated by backspace + // quoted input to avoid special char to be interpreted + var cmd = 'gm identify -format "%p " "' + input + '"'; var execSync = require('child_process').execSync; - var pageCount = parseInt(execSync(cmd).toString().trim()); - callback(null, pageCount) - }, - - function (pageCount, callback) { - var pages = []; - if (!pageCount) - callback('[Error: Invalid page number]', null); + // get all pages number and fill them to array + var pages = execSync(cmd).toString().match(/[0-9]+/g); - for (var i = 1; i <= pageCount; i++) { - pages.push(i); - - if (i == pageCount) callback(null, pages); + if (!pages.length) { + callback('[Error: Empty file]', null); + return; } + + callback(null, pages); }, function (pages, callback) { diff --git a/test/index.js b/test/index.js index 4e5ea2d..ce861da 100644 --- a/test/index.js +++ b/test/index.js @@ -11,28 +11,28 @@ pdf2img.setOptions({ targetname: 'test' }); -describe('Split and covert pdf into images', function() { +describe('Split and convert pdf into images', function() { it ('Create png files', function(done) { - this.timeout(100000); + this.timeout(10000); pdf2img.convert(input, function(err, info) { var n = 1; info.forEach(function(file) { - file.page.should.equal(n); + file.page.should.equal(n+''); file.name.should.equal('test_' + n + '.png'); - if (n === 3) done(); + if (n == 3) done(); n++; }); }); }); it ('Create jpg files', function(done) { - this.timeout(100000); + this.timeout(10000); pdf2img.setOptions({ type: 'jpg' }); pdf2img.convert(input, function(err, info) { var n = 1; info.forEach(function(file) { - file.page.should.equal(n); + file.page.should.equal(n+''); file.name.should.equal('test_' + n + '.jpg'); - if (n === 3) done(); + if (n == 3) done(); n++; }); });