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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ pdf2img.setOptions({
size: 1024, // default 1024
density: 600, // default 600
outputdir: __dirname + path.sep + 'output', // output folder, default null (if null given, then it will create folder name same as file name)
outputname: 'test', // output file name, dafault null (if null given, then it will create image name same as input name)
page: null // convert selected page, default null (if null given, then it will convert all pages)
outputname: 'test', // output file name, default null (if null given, then it will create image name same as input name)
page: null, // convert selected page, default null (if null given, then it will convert all pages)
startindex: 1 // index to start width, default 1 (if not present, will start at 0)
});

pdf2img.convert(input, function(err, info) {
Expand Down
61 changes: 30 additions & 31 deletions lib/pdf2img.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var options = {
density: 600,
outputdir: null,
outputname: null,
page: null
page: null,
startindex: 1
};

var Pdf2Img = function() {};
Expand All @@ -23,6 +24,7 @@ Pdf2Img.prototype.setOptions = function(opts) {
options.outputdir = opts.outputdir || options.outputdir;
options.outputname = opts.outputname || options.outputname;
options.page = opts.page || options.page;
options.startindex = opts.startindex || options.startindex;
};

Pdf2Img.prototype.convert = function(input, callbackreturn) {
Expand Down Expand Up @@ -66,49 +68,46 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) {

async.waterfall([
// Get pages count
function (callback) {
gm(input).identify("%p ", function (err, value) {
var pageCount = String(value).split(' ');

if (!pageCount.length) {
callback({
result: 'error',
message: 'Invalid page number.'
}, null);
} else {
// Convert selected page
if (options.page !== null) {
if (options.page < pageCount.length) {
callback(null, [options.page]);
} else {
callback({
result: 'error',
message: 'Invalid page number.'
}, null);
}
} else {
callback(null, pageCount);
}
}

})

},
function(callback) {
var cmd = 'gm identify -format "%p " "' + input + '"';
var execSync = require('child_process').execSync;
var pageCount = execSync(cmd).toString().match(/[0-9]+/g);

if (!pageCount.length) {
return callback({
result: 'error',
message: 'Invalid page number.'
}, null);
}

// Convert selected page
if (options.page !== null) {
if (options.page < pageCount.length) {
return callback(null, [options.page]);
} else {
return callback({
result: 'error',
message: 'Invalid page number.'
}, null);
}
}

return callback(null, pageCount);
},

// Convert pdf file
function(pages, callback) {
// Use eachSeries to make sure that conversion has done page by page
async.eachSeries(pages, function(page, callbackmap) {
var inputStream = fs.createReadStream(input);
var outputFile = options.outputdir + options.outputname + '_' + page + '.' + options.type;
var outputFile = options.outputdir + options.outputname + '_' + (+page + options.startindex - 1) + '.' + options.type;

convertPdf2Img(inputStream, outputFile, parseInt(page), function(error, result) {
if (error) {
return callbackmap(error);
}

stdout.push(result);
callbackreturn(null, {result: 'page_processed', message: result});
return callbackmap(error, result);
});
}, function(e) {
Expand Down