From 2f06a2bfebcefe467bb0fd19799b786588f8aa43 Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 15 May 2017 00:01:34 +0700 Subject: [PATCH 01/16] Update version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ae215a3..299b507 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pdf2img", - "version": "0.2.0", + "version": "0.3.0", "description": "A nodejs module for converting pdf into image", "author": "Fitra Aditya ", "license": "MIT", From 764a48af500a133a780a9fbcb3e28e8cb9add980 Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 15 May 2017 00:05:36 +0700 Subject: [PATCH 02/16] Update README. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index dcab607..7044234 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ A nodejs module for converting pdf into image file +## Dependencies +- ImageMagick +- GraphicsMagick + +**Note: Currently it only works on Linux.** + ## Installation ``` $ [sudo] npm install pdf2img From 9e5c177e2118aefd64f1c705185c93f4abbc102b Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 15 May 2017 01:00:12 +0700 Subject: [PATCH 03/16] Update README, remove ImageMagick as dependency. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 7044234..8f4eaeb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ A nodejs module for converting pdf into image file ## Dependencies -- ImageMagick - GraphicsMagick **Note: Currently it only works on Linux.** From fc3a3718f9f7f543be17ea64a8c429d2eddfcf13 Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 15 May 2017 01:00:24 +0700 Subject: [PATCH 04/16] Bump version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 299b507..897e462 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pdf2img", - "version": "0.3.0", + "version": "0.4.0", "description": "A nodejs module for converting pdf into image", "author": "Fitra Aditya ", "license": "MIT", From b75d5c362409758f806a689a7781f6fc16e6d389 Mon Sep 17 00:00:00 2001 From: Julien Bouquillon Date: Fri, 2 Jun 2017 11:15:29 +0200 Subject: [PATCH 05/16] feature: add options.page --- lib/pdf2img.js | 8 +++++++- test/index.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index edd53a7..525aaf1 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -10,7 +10,8 @@ var options = { size: 1024, density: 600, outputdir: null, - outputname: null + outputname: null, + page: undefined }; var Pdf2Img = function() {}; @@ -18,6 +19,7 @@ var Pdf2Img = function() {}; Pdf2Img.prototype.setOptions = function(opts) { options.type = opts.type || options.type; options.size = opts.size || options.size; + options.page = opts.page || options.page; options.density = opts.density || options.density; options.outputdir = opts.outputdir || options.outputdir; options.outputname = opts.outputname || options.outputname; @@ -65,6 +67,10 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { async.waterfall([ // Get pages count function(callback) { + if (options.page !== undefined) { + callback(null, [options.page]); + return; + } var cmd = 'gm identify -format "%p " "' + input + '"'; var execSync = require('child_process').execSync; var pageCount = execSync(cmd).toString().match(/[0-9]+/g); diff --git a/test/index.js b/test/index.js index e3b49fa..ac54876 100644 --- a/test/index.js +++ b/test/index.js @@ -51,6 +51,23 @@ describe('Split and convert pdf into images', function() { } }); }); + it ('Create jpg file only for given page', function(done) { + this.timeout(100000); + pdf2img.setOptions({ type: 'jpg', page: 2 }); + pdf2img.convert(input, function(err, info) { + if (info.result !== 'success') { + info.result.should.equal('success'); + done(); + } else { + info.message.length.should.equal(1) + var file = info.message[0]; + file.page.should.equal(2); + file.name.should.equal('test_2.jpg'); + isFileExists(file.path).should.to.be.true; + done(); + } + }); + }); }); var isFileExists = function(path) { From abe6e81b25d0a83b630807216fd095d53dd26dd1 Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Sun, 4 Jun 2017 23:55:14 +0700 Subject: [PATCH 06/16] Check given selected page and total page number. --- lib/pdf2img.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index 525aaf1..2faae8c 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -11,7 +11,7 @@ var options = { density: 600, outputdir: null, outputname: null, - page: undefined + page: null }; var Pdf2Img = function() {}; @@ -19,10 +19,10 @@ var Pdf2Img = function() {}; Pdf2Img.prototype.setOptions = function(opts) { options.type = opts.type || options.type; options.size = opts.size || options.size; - options.page = opts.page || options.page; options.density = opts.density || options.density; options.outputdir = opts.outputdir || options.outputdir; options.outputname = opts.outputname || options.outputname; + options.page = opts.page || options.page; }; Pdf2Img.prototype.convert = function(input, callbackreturn) { @@ -67,21 +67,29 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { async.waterfall([ // Get pages count function(callback) { - if (options.page !== undefined) { - callback(null, [options.page]); - return; - } 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) { - callback({ + 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); + } + } + callback(null, pageCount); }, From a0009cf9b1adee35768075aecd998d45a551f9b6 Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 5 Jun 2017 00:02:16 +0700 Subject: [PATCH 07/16] Make callback return. --- lib/pdf2img.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index 2faae8c..db17ea4 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -90,7 +90,7 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { } } - callback(null, pageCount); + return callback(null, pageCount); }, // Convert pdf file @@ -102,16 +102,16 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { convertPdf2Img(inputStream, outputFile, parseInt(page), function(error, result) { if (error) { - callbackmap(error); + return callbackmap(error); } stdout.push(result); - callbackmap(error, result); + return callbackmap(error, result); }); }, function(e) { if (e) callback(e); - callback(null, { + return callback(null, { result: 'success', message: stdout }); @@ -124,7 +124,7 @@ var convertPdf2Img = function(input, output, page, callback) { if (input.path) { var filepath = input.path; } else { - callback({ + return callback({ result: 'error', message: 'Invalid input file path.' }, null); @@ -138,14 +138,14 @@ var convertPdf2Img = function(input, output, page, callback) { .quality(100) .write(output, function(err) { if (err) { - callback({ + return callback({ result: 'error', message: 'Can not write output file.' }, null); } if (!(fs.statSync(output)['size'] / 1000)) { - callback({ + return callback({ result: 'error', message: 'Zero sized output image detected.' }, null); @@ -158,7 +158,7 @@ var convertPdf2Img = function(input, output, page, callback) { path: output }; - callback(null, results); + return callback(null, results); }); }; From 8bce0adb0ed67a456db91dc30c27979940387fab Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 5 Jun 2017 00:02:38 +0700 Subject: [PATCH 08/16] Update test. --- test/index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index ac54876..f5dad9b 100644 --- a/test/index.js +++ b/test/index.js @@ -53,7 +53,24 @@ describe('Split and convert pdf into images', function() { }); it ('Create jpg file only for given page', function(done) { this.timeout(100000); - pdf2img.setOptions({ type: 'jpg', page: 2 }); + pdf2img.setOptions({ type: 'jpg', page: 1 }); + pdf2img.convert(input, function(err, info) { + if (info.result !== 'success') { + info.result.should.equal('success'); + done(); + } else { + info.message.length.should.equal(1) + var file = info.message[0]; + file.page.should.equal(1); + file.name.should.equal('test_1.jpg'); + isFileExists(file.path).should.to.be.true; + done(); + } + }); + }); + it ('Create png file only for given page', function(done) { + this.timeout(100000); + pdf2img.setOptions({ type: 'png', page: 2 }); pdf2img.convert(input, function(err, info) { if (info.result !== 'success') { info.result.should.equal('success'); @@ -62,7 +79,7 @@ describe('Split and convert pdf into images', function() { info.message.length.should.equal(1) var file = info.message[0]; file.page.should.equal(2); - file.name.should.equal('test_2.jpg'); + file.name.should.equal('test_2.png'); isFileExists(file.path).should.to.be.true; done(); } From be8fe5434af78884061c84007e415c5f41f30c7b Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 5 Jun 2017 00:04:54 +0700 Subject: [PATCH 09/16] Update readme. --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8f4eaeb..f9c2810 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,8 @@ 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) + 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) }); pdf2img.convert(input, function(err, info) { @@ -54,11 +55,6 @@ It will return array of splitted and converted image files. path: '/output/test_3.jpg' } ] } ``` -Note that pdf2img will split and convert all pages. - -## To Do -* Convert selected pages - ## Maintainer [Fitra Aditya][0] From 3a5f48c36c722dc31237b358834a57b2154ab3ed Mon Sep 17 00:00:00 2001 From: Fitra Aditya Date: Mon, 5 Jun 2017 00:05:17 +0700 Subject: [PATCH 10/16] Bump new version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 897e462..75778f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pdf2img", - "version": "0.4.0", + "version": "0.5.0", "description": "A nodejs module for converting pdf into image", "author": "Fitra Aditya ", "license": "MIT", From f65c79558e65b819cb2c2b4fc95a6007051a597d Mon Sep 17 00:00:00 2001 From: Vladislav Stoitsov Date: Wed, 12 Jul 2017 00:47:16 +0300 Subject: [PATCH 11/16] Fixed using gm as node_module to identify pages --- lib/pdf2img.js | 53 ++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index db17ea4..9c2e9a8 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -66,32 +66,35 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { async.waterfall([ // Get pages count - function(callback) { - var cmd = 'gm identify -format "%p " "' + input + '"'; - var execSync = require('child_process').execSync; - var pageCount = execSync(cmd).toString().match(/[0-9]+/g); + 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); + } + } + + }) + + }, - 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) { From 2c9664d38a7f9180656fff1a2a3921325e0967cd Mon Sep 17 00:00:00 2001 From: artcoding-git Date: Sun, 20 Aug 2017 18:50:28 +0300 Subject: [PATCH 12/16] Update README.md It works fine on Windows (checked). Put link how required GraphicsMagick with Ghostscript can be installed on Windows. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9c2810..2f08c1a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A nodejs module for converting pdf into image file ## Dependencies - GraphicsMagick -**Note: Currently it only works on Linux.** +Note: Windows users, please be sure GraphicsMagick and Ghostscript are installed (see https://stackoverflow.com/questions/18733695/cimg-error-gm-exe-is-not-recognized-as-an-internal-or-external-command/45783910#45783910 for details) - then it works fine on Windows. ## Installation ``` From f7a4b42e6b2ef266218993d26c4f7cf3c96fce91 Mon Sep 17 00:00:00 2001 From: Charmie Date: Sat, 25 Nov 2017 00:44:21 +0800 Subject: [PATCH 13/16] Feature: Convert multiple pdf files - Remove or assign on options to avoid overwriting options whenever Pdf2Img.prototype.convert is called. - Added function Pdf2Img.prototype.convertList() to convert list of pdf files in array. 1st argument is the array, 2nd argument is the "options" object. --- lib/pdf2img.js | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index edd53a7..cf0f6ec 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -16,11 +16,11 @@ var options = { var Pdf2Img = function() {}; Pdf2Img.prototype.setOptions = function(opts) { - options.type = opts.type || options.type; - options.size = opts.size || options.size; - options.density = opts.density || options.density; - options.outputdir = opts.outputdir || options.outputdir; - options.outputname = opts.outputname || options.outputname; + options.type = opts.type; + options.size = opts.size; + options.density = opts.density; + options.outputdir = opts.outputdir; + options.outputname = opts.outputname; }; Pdf2Img.prototype.convert = function(input, callbackreturn) { @@ -166,4 +166,31 @@ var isFileExists = function(path) { } } + +// Convert list of pdf files, 1st argument is array of pdf files, 2nd argument need to pass opts object to set options variable +Pdf2Img.prototype.convertList = function(arr, opts, callbackreturn) { + async.eachSeries(arr, function(file, callback) { + Pdf2Img.prototype.setOptions(opts); + Pdf2Img.prototype.convert(file, function(err, info) { + if(err) { + return callback({ + result: 'error', + message: 'Unable to convert file ' + file + }, null); + } else { + return callback(null, info); + } + }); + }, function(err) { + if(err) { + return callbackreturn({ + result: 'error', + message: 'Unable to convert everything on array' + }); + } else { + return callbackreturn(null, 'All files have been converted'); + } + }); +}; + module.exports = new Pdf2Img; From 36b03ec49f77526cf7e21f9e4c44d2428e869426 Mon Sep 17 00:00:00 2001 From: Charmie Date: Sat, 25 Nov 2017 01:51:55 +0800 Subject: [PATCH 14/16] Allow multiple pdf files to be converted - Remove "or" assignment on Pdf2Img.prototype.setOptions() to avoid overwriting options object whenever Pdf2Img.prototype.convert is called. - Added function Pdf2Img.prototype.convertList() to convert list of pdf files in array. 1st argument is the array, 2nd argument is the "options" object. --- lib/pdf2img.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index cf0f6ec..7e74e2c 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -174,21 +174,21 @@ Pdf2Img.prototype.convertList = function(arr, opts, callbackreturn) { Pdf2Img.prototype.convert(file, function(err, info) { if(err) { return callback({ - result: 'error', - message: 'Unable to convert file ' + file + error: 'Unable to convert file ' + file, + message: err }, null); } else { - return callback(null, info); + callback(null, info); } }); }, function(err) { if(err) { return callbackreturn({ - result: 'error', - message: 'Unable to convert everything on array' - }); + error: 'Unable to convert everything on array', + message: err, + }, null); } else { - return callbackreturn(null, 'All files have been converted'); + callbackreturn(null, 'All files have been converted'); } }); }; From 1df753185ddfac09728ea3cf0b815e3c67f78d52 Mon Sep 17 00:00:00 2001 From: Charmie Date: Sat, 25 Nov 2017 02:02:44 +0800 Subject: [PATCH 15/16] Allow multiple pdf files to be converted - Remove "or" assignment on Pdf2Img.prototype.setOptions() to avoid overwriting options object whenever Pdf2Img.prototype.convert is called. - Added function Pdf2Img.prototype.convertList() to convert list of pdf files in array. 1st argument is the array, 2nd argument is the "options" object. --- lib/pdf2img.js | 81 +++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 53 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index 7d30e37..7e74e2c 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -10,27 +10,17 @@ var options = { size: 1024, density: 600, outputdir: null, - outputname: null, - page: null + outputname: null }; var Pdf2Img = function() {}; Pdf2Img.prototype.setOptions = function(opts) { -<<<<<<< HEAD options.type = opts.type; options.size = opts.size; options.density = opts.density; options.outputdir = opts.outputdir; options.outputname = opts.outputname; -======= - options.type = opts.type || options.type; - options.size = opts.size || options.size; - options.density = opts.density || options.density; - options.outputdir = opts.outputdir || options.outputdir; - options.outputname = opts.outputname || options.outputname; - options.page = opts.page || options.page; ->>>>>>> refs/remotes/origin/master }; Pdf2Img.prototype.convert = function(input, callbackreturn) { @@ -74,35 +64,20 @@ 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) { + callback({ + result: 'error', + message: 'Invalid page number.' + }, null); + } + + callback(null, pageCount); + }, // Convert pdf file function(pages, callback) { @@ -113,16 +88,16 @@ Pdf2Img.prototype.convert = function(input, callbackreturn) { convertPdf2Img(inputStream, outputFile, parseInt(page), function(error, result) { if (error) { - return callbackmap(error); + callbackmap(error); } stdout.push(result); - return callbackmap(error, result); + callbackmap(error, result); }); }, function(e) { if (e) callback(e); - return callback(null, { + callback(null, { result: 'success', message: stdout }); @@ -135,7 +110,7 @@ var convertPdf2Img = function(input, output, page, callback) { if (input.path) { var filepath = input.path; } else { - return callback({ + callback({ result: 'error', message: 'Invalid input file path.' }, null); @@ -149,14 +124,14 @@ var convertPdf2Img = function(input, output, page, callback) { .quality(100) .write(output, function(err) { if (err) { - return callback({ + callback({ result: 'error', message: 'Can not write output file.' }, null); } if (!(fs.statSync(output)['size'] / 1000)) { - return callback({ + callback({ result: 'error', message: 'Zero sized output image detected.' }, null); @@ -169,7 +144,7 @@ var convertPdf2Img = function(input, output, page, callback) { path: output }; - return callback(null, results); + callback(null, results); }); }; @@ -199,21 +174,21 @@ Pdf2Img.prototype.convertList = function(arr, opts, callbackreturn) { Pdf2Img.prototype.convert(file, function(err, info) { if(err) { return callback({ - result: 'error', - message: 'Unable to convert file ' + file + error: 'Unable to convert file ' + file, + message: err }, null); } else { - return callback(null, info); + callback(null, info); } }); }, function(err) { if(err) { return callbackreturn({ - result: 'error', - message: 'Unable to convert everything on array' - }); + error: 'Unable to convert everything on array', + message: err, + }, null); } else { - return callbackreturn(null, 'All files have been converted'); + callbackreturn(null, 'All files have been converted'); } }); }; From 0c70b8fcf797e8fe0d7e272f202f2ec548c7f1bc Mon Sep 17 00:00:00 2001 From: Charmie Quino Date: Fri, 12 Jan 2018 19:30:02 +0800 Subject: [PATCH 16/16] updated assignment on options --- lib/pdf2img.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pdf2img.js b/lib/pdf2img.js index 7e74e2c..7db50f0 100644 --- a/lib/pdf2img.js +++ b/lib/pdf2img.js @@ -16,11 +16,11 @@ var options = { var Pdf2Img = function() {}; Pdf2Img.prototype.setOptions = function(opts) { - options.type = opts.type; - options.size = opts.size; - options.density = opts.density; - options.outputdir = opts.outputdir; - options.outputname = opts.outputname; + options.type = opts.type != null || opts.type != 'undefined' ? opts.type : options.type; + options.size = opts.size != null || opts.size != 'undefined' ? opts.size : options.size; + options.density = opts.density != null || opts.density != 'undefined' ? opts.density : options.density; + options.outputdir = opts.outputdir != null || opts.outputdir != 'undefined' ? opts.outputdir : options.outputdir; + options.outputname = opts.outputname != null || opts.outputname != 'undefined' ? opts.outputname : options.outputname; }; Pdf2Img.prototype.convert = function(input, callbackreturn) {