From 9b38c43f61cd0b9c25bfb5655be91406c5d6a232 Mon Sep 17 00:00:00 2001 From: agamemnus Date: Mon, 12 Mar 2018 06:06:37 -0400 Subject: [PATCH 1/4] Add startindex option The `startindex` option starts pages from a specified index (default 1). Useful if you want to start pages from 0... --- lib/pdf2img.js | 59 +++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index 9c2e9a8..c92e1d0 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -11,7 +11,8 @@ var options = { density: 600, outputdir: null, outputname: null, - page: null + page: null, + startindex: 1 }; var Pdf2Img = function() {}; @@ -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) { @@ -66,42 +68,39 @@ 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) { From 96fc5662ed3da55361ae9d4dbfd71d9afe530ed7 Mon Sep 17 00:00:00 2001 From: agamemnus Date: Mon, 12 Mar 2018 06:10:58 -0400 Subject: [PATCH 2/4] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2f08c1a..b9d6362 100644 --- a/README.md +++ b/README.md @@ -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) { From 26064cf47163e6b71683a9f86521eda7aede94f8 Mon Sep 17 00:00:00 2001 From: agamemnus Date: Mon, 12 Mar 2018 06:17:02 -0400 Subject: [PATCH 3/4] Update pdf2img.js --- lib/pdf2img.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index c92e1d0..491b816 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -100,7 +100,7 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { // 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.startindex - 1) + '.' + options.type; + var outputFile = options.outputdir + options.outputname + '_' + (+page + options.startindex - 1) + '.' + options.type; convertPdf2Img(inputStream, outputFile, parseInt(page), function(error, result) { if (error) { From 04b542921fe77bb0cf35b855a11eba89eff5e00f Mon Sep 17 00:00:00 2001 From: agamemnus Date: Wed, 9 May 2018 01:16:07 -0400 Subject: [PATCH 4/4] Add a per-page processed callback inside async.eachSeries. --- lib/pdf2img.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index 491b816..392b699 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -106,8 +106,8 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { if (error) { return callbackmap(error); } - stdout.push(result); + callbackreturn(null, {result: 'page_processed', message: result}); return callbackmap(error, result); }); }, function(e) {