From db608026ba8fee7c7221f781ba1236ab68c94e41 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 30 Nov 2016 01:38:47 +0100 Subject: [PATCH 1/3] Fix special char on name bugs --- lib/pdf2img.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index dbd050a..e5c410e 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -42,10 +42,12 @@ 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) + callback(null, pageCount); }, function (pageCount, callback) { From 2dad8b49fa06d51f01e4f6191fb918dbb9decd93 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 30 Nov 2016 01:41:44 +0100 Subject: [PATCH 2/3] Fix infinity page loading issue --- lib/pdf2img.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index e5c410e..1e42048 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -46,20 +46,15 @@ Pdf2Img.prototype.convert = function (file, callbackreturn) { // 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) { From ad8cfb1c33b601bc70a907ffa7b192a68d0929bd Mon Sep 17 00:00:00 2001 From: = Date: Wed, 30 Nov 2016 02:34:41 +0100 Subject: [PATCH 3/3] Fix string to int unit test comparison --- test/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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++; }); });