Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions lib/pdf2img.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
});
});
Expand Down