diff --git a/README.md b/README.md index dcab607..2f08c1a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ A nodejs module for converting pdf into image file +## Dependencies +- GraphicsMagick + +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 ``` $ [sudo] npm install pdf2img @@ -21,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) { @@ -49,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] diff --git a/lib/pdf2img.js b/lib/pdf2img.js index edd53a7..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.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 != 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) { @@ -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({ + error: 'Unable to convert file ' + file, + message: err + }, null); + } else { + callback(null, info); + } + }); + }, function(err) { + if(err) { + return callbackreturn({ + error: 'Unable to convert everything on array', + message: err, + }, null); + } else { + callbackreturn(null, 'All files have been converted'); + } + }); +}; + module.exports = new Pdf2Img; diff --git a/package.json b/package.json index ae215a3..75778f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pdf2img", - "version": "0.2.0", + "version": "0.5.0", "description": "A nodejs module for converting pdf into image", "author": "Fitra Aditya ", "license": "MIT", diff --git a/test/index.js b/test/index.js index e3b49fa..f5dad9b 100644 --- a/test/index.js +++ b/test/index.js @@ -51,6 +51,40 @@ 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: 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'); + done(); + } else { + info.message.length.should.equal(1) + var file = info.message[0]; + file.page.should.equal(2); + file.name.should.equal('test_2.png'); + isFileExists(file.path).should.to.be.true; + done(); + } + }); + }); }); var isFileExists = function(path) {