Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple pdf files to be converted #40

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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]

Expand Down
37 changes: 32 additions & 5 deletions lib/pdf2img.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ var options = {
var Pdf2Img = function() {};

Pdf2Img.prototype.setOptions = function(opts) {
options.type = opts.type || options.type;
Copy link
Contributor

@elhigu elhigu Dec 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right, now if options has already value and it is not given in opts it will be overridden with undefined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I made another commit that should handle assignments on the options variable.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is broken in so many levels that I don't know where to start. Why have you changed these line at all? AFAIK the original implementation was correct.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to edit the lib to be used in converting list of PDF files, so far it's working on my end. Most likely my code is not clean or should have better error handling. Added a new function at the bottom that handles an array of PDF where "options" can be passed. Oh well, will need to re-visit this next time.

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) {
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
Expand Down
34 changes: 34 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down