Skip to content

Commit

Permalink
Merge pull request mooz#36 from roest01/feature/single-image-result
Browse files Browse the repository at this point in the history
Feature to convert whole file and combine output images
  • Loading branch information
mooz authored May 7, 2018
2 parents 3809535 + 31edb6b commit 039ac28
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 10 deletions.
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Ensure you have `convert`, `gs`, and `pdfinfo` (part of poppler) commands.

## Usage

#### Convert single page:
```javascript
var PDFImage = require("pdf-image").PDFImage;

Expand All @@ -29,6 +30,29 @@ pdfImage.convertPage(0).then(function (imagePath) {
});
```

#### Convert full file
```javascript
var PDFImage = require("pdf-image").PDFImage;

var pdfImage = new PDFImage("/tmp/slide.pdf");
pdfImage.convertFile().then(function (imagePaths) {
// [ /tmp/slide-0.png, /tmp/slide-1.png ]
});


```
#### Convert full file and merge result into single image
```javascript
var PDFImage = require("pdf-image").PDFImage;
var pdfImage = new PDFImage("/tmp/slide.pdf", {
combinedImage: true
});

pdfImage.convertFile().then(function (imagePaths) {
// /tmp/slide.png
});
```

## Express

Following example shows an example of pdf-image in Express, which gives
Expand Down Expand Up @@ -56,14 +80,10 @@ URLs for each pages of a PDF like
Following example shows an example of how to add imagemagick command-line options (you can find the complete list here -> http://www.imagemagick.org/script/convert.php):

```javascript
var theOptions = {};
theOptions.convertOptions = {};
theOptions.convertOptions["-resize"] = "2000x2000";
theOptions.convertOptions["-quality"] = "75";
```

And then just change the instantiation of a new PDFImage by:

```javascript
var pdfImage = new PDFImage(pdfPath, theOptions);
var pdfImage = new PDFImage(pdfPath, {
convertOptions: {
"-resize": "2000x2000",
"-quality": "75"
}
});
```
66 changes: 66 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function PDFImage(pdfFilePath, options) {
this.setConvertOptions(options.convertOptions);
this.setConvertExtension(options.convertExtension);
this.useGM = options.graphicsMagick || false;
this.combinedImage = options.combinedImage || false;

this.outputDirectory = options.outputDirectory || path.dirname(pdfFilePath);
}
Expand Down Expand Up @@ -65,6 +66,12 @@ PDFImage.prototype = {
this.pdfFileBaseName + "-" + pageNumber + "." + this.convertExtension
);
},
getOutputImagePathForFile: function () {
return path.join(
this.outputDirectory,
this.pdfFileBaseName + "." + this.convertExtension
);
},
setConvertOptions: function (convertOptions) {
this.convertOptions = convertOptions || {};
},
Expand All @@ -85,6 +92,14 @@ PDFImage.prototype = {
pdfFilePath, pageNumber, outputImagePath
);
},
constructCombineCommandForFile: function (imagePaths) {
return util.format(
"%s -append %s \"%s\"",
this.useGM ? "gm convert" : "convert",
imagePaths.join(' '),
this.getOutputImagePathForFile()
);
},
constructConvertOptions: function () {
return Object.keys(this.convertOptions).sort().map(function (optionName) {
if (this.convertOptions[optionName] !== null) {
Expand All @@ -94,6 +109,57 @@ PDFImage.prototype = {
}
}, this).join(" ");
},
combineImages: function(imagePaths) {
var pdfImage = this;
var combineCommand = pdfImage.constructCombineCommandForFile(imagePaths);
return new Promise(function (resolve, reject) {
exec(combineCommand, function (err, stdout, stderr) {
if (err) {
return reject({
message: "Failed to combine images",
error: err,
stdout: stdout,
stderr: stderr
});
}
exec("rm "+imagePaths.join(' ')); //cleanUp
return resolve(pdfImage.getOutputImagePathForFile());
});
});
},
convertFile: function () {
var pdfImage = this;
return new Promise(function (resolve, reject) {
pdfImage.numberOfPages().then(function (totalPages) {
var convertPromise = new Promise(function (resolve, reject){
var imagePaths = [];
for (var i = 0; i < totalPages; i++) {
pdfImage.convertPage(i).then(function(imagePath){
imagePaths.push(imagePath);
if (imagePaths.length === parseInt(totalPages)){
imagePaths.sort(); //because of asyc pages we have to reSort pages
resolve(imagePaths);
}
}).catch(function(error){
reject(error);
});
}
});

convertPromise.then(function(imagePaths){
if (pdfImage.combinedImage){
pdfImage.combineImages(imagePaths).then(function(imagePath){
resolve(imagePath);
});
} else {
resolve(imagePaths);
}
}).catch(function(error){
reject(error);
});
});
});
},
convertPage: function (pageNumber) {
var pdfFilePath = this.pdfFilePath;
var outputImagePath = this.getOutputImagePathForPage(pageNumber);
Expand Down
39 changes: 39 additions & 0 deletions tests/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe("PDFImage", function () {
let pdfPath = "/tmp/test.pdf";
let pdfImage;
let generatedFiles = [];
this.timeout(7000);

before(function(done){
fs.createReadStream('tests/test.pdf').pipe(fs.createWriteStream(pdfPath));
Expand Down Expand Up @@ -39,13 +40,20 @@ describe("PDFImage", function () {
.equal("/tmp/test-2.png");
expect(pdfImage.getOutputImagePathForPage(1000))
.equal("/tmp/test-1000.png");
expect(pdfImage.getOutputImagePathForFile())
.equal("/tmp/test.png");
});

it("should return correct convert command", function () {
expect(pdfImage.constructConvertCommandForPage(1))
.equal('convert "/tmp/test.pdf[1]" "/tmp/test-1.png"');
});

it("should return correct convert command to combine images", function () {
expect(pdfImage.constructCombineCommandForFile(['/tmp/test-0.png', '/tmp/test-1.png']))
.equal('convert -append /tmp/test-0.png /tmp/test-1.png "/tmp/test.png"');
});

it("should use gm when you ask it to", function () {
pdfImage = new PDFImage(pdfPath, {graphicsMagick: true});
expect(pdfImage.constructConvertCommandForPage(1))
Expand Down Expand Up @@ -93,6 +101,37 @@ describe("PDFImage", function () {
});
});

it("should convert all PDF's pages to files", function () {
return new Promise(function(resolve, reject) {
pdfImage.convertFile().then(function (imagePaths) {
imagePaths.forEach(function(imagePath){
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
});
resolve();
}).catch(function(err){
reject(err);
});
});
});

it("should convert all PDF's pages to single image", function () {
return new Promise(function(resolve, reject){
let pdfImageCombined = new PDFImage(pdfPath, {
combinedImage: true,
});

pdfImageCombined.convertFile().then(function (imagePath) {
expect(imagePath).to.equal("/tmp/test.png");
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
resolve();
}).catch(function (error) {
reject(error);
});
})
});

it("should return # of pages", function () {
return new Promise(function(resolve, reject) {
pdfImage.numberOfPages().then(function (numberOfPages) {
Expand Down

0 comments on commit 039ac28

Please sign in to comment.