Comparing version 1.1.0 to 2.0.0
75
index.js
@@ -14,7 +14,8 @@ // node-pdf | ||
this.pdfFilePath = pdfFilePath; | ||
this.pdfFileBaseName = path.basename(pdfFilePath, ".pdf"); | ||
this.setPdfFileBaseName(options.pdfFileBaseName); | ||
this.setConvertOptions(options.convertOptions); | ||
this.setConvertExtension(options.convertExtension); | ||
this.useGM = options.graphicsMagick || false; | ||
this.combinedImage = options.combinedImage || false; | ||
@@ -27,3 +28,3 @@ this.outputDirectory = options.outputDirectory || path.dirname(pdfFilePath); | ||
return util.format( | ||
"pdfinfo '%s'", | ||
"pdfinfo \"%s\"", | ||
this.pdfFilePath | ||
@@ -70,5 +71,14 @@ ); | ||
}, | ||
getOutputImagePathForFile: function () { | ||
return path.join( | ||
this.outputDirectory, | ||
this.pdfFileBaseName + "." + this.convertExtension | ||
); | ||
}, | ||
setConvertOptions: function (convertOptions) { | ||
this.convertOptions = convertOptions || {}; | ||
}, | ||
setPdfFileBaseName: function(pdfFileBaseName) { | ||
this.pdfFileBaseName = pdfFileBaseName || path.basename(this.pdfFilePath, ".pdf"); | ||
}, | ||
setConvertExtension: function (convertExtension) { | ||
@@ -82,3 +92,3 @@ this.convertExtension = convertExtension || "png"; | ||
return util.format( | ||
"%s %s'%s[%d]' '%s'", | ||
"%s %s\"%s[%d]\" \"%s\"", | ||
this.useGM ? "gm convert" : "convert", | ||
@@ -89,2 +99,10 @@ convertOptionsString ? convertOptionsString + " " : "", | ||
}, | ||
constructCombineCommandForFile: function (imagePaths) { | ||
return util.format( | ||
"%s -append %s \"%s\"", | ||
this.useGM ? "gm convert" : "convert", | ||
imagePaths.join(' '), | ||
this.getOutputImagePathForFile() | ||
); | ||
}, | ||
constructConvertOptions: function () { | ||
@@ -99,2 +117,53 @@ return Object.keys(this.convertOptions).sort().map(function (optionName) { | ||
}, | ||
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) { | ||
@@ -101,0 +170,0 @@ var pdfFilePath = this.pdfFilePath; |
{ | ||
"name": "pdf-image", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"main": "index.js", | ||
@@ -14,7 +14,7 @@ "repository": { | ||
"dependencies": { | ||
"es6-promise": "~2.0.0", | ||
"chai": "~1.9.2" | ||
"es6-promise": "~4.2.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~2.3.4" | ||
"chai": "~4.1.2", | ||
"mocha": "~5.1.1" | ||
}, | ||
@@ -21,0 +21,0 @@ "scripts": { |
@@ -22,2 +22,3 @@ # pdf-image | ||
#### Convert single page: | ||
```javascript | ||
@@ -33,2 +34,25 @@ var PDFImage = require("pdf-image").PDFImage; | ||
#### 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 | ||
@@ -55,1 +79,14 @@ | ||
``` | ||
## Options | ||
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 pdfImage = new PDFImage(pdfPath, { | ||
convertOptions: { | ||
"-resize": "2000x2000", | ||
"-quality": "75" | ||
} | ||
}); | ||
``` |
@@ -1,12 +0,23 @@ | ||
var assert = require("assert"); | ||
var expect = require("chai").expect; | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
let expect = require("chai").expect; | ||
let fs = require("fs"); | ||
var PDFImage = require("../").PDFImage; | ||
let PDFImage = require("../").PDFImage; | ||
describe("PDFImage", function () { | ||
var pdfPath = "/tmp/test.pdf"; | ||
var pdfImage; | ||
let pdfPath = "/tmp/test.pdf"; | ||
let pdfImage; | ||
let generatedFiles = []; | ||
this.timeout(7000); | ||
before(function(done){ | ||
fs.createReadStream('tests/test.pdf').pipe(fs.createWriteStream(pdfPath)); | ||
if (fs.existsSync(pdfPath)){ | ||
done(); | ||
} else { | ||
throw new Error({ | ||
message: 'File missing at: '+ pdfPath + '. Copy task was not a success' | ||
}); | ||
} | ||
}); | ||
beforeEach(function() { | ||
@@ -19,2 +30,7 @@ pdfImage = new PDFImage(pdfPath) | ||
}); | ||
it("should set custom basename", function() { | ||
pdfImage.setPdfFileBaseName('custom-basename'); | ||
expect(pdfImage.pdfFileBaseName).equal("custom-basename"); | ||
}); | ||
@@ -28,2 +44,4 @@ it("should return correct page path", function () { | ||
.equal("/tmp/test-1000.png"); | ||
expect(pdfImage.getOutputImagePathForFile()) | ||
.equal("/tmp/test.png"); | ||
}); | ||
@@ -33,9 +51,14 @@ | ||
expect(pdfImage.constructConvertCommandForPage(1)) | ||
.equal("convert '/tmp/test.pdf[1]' '/tmp/test-1.png'"); | ||
.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}) | ||
pdfImage = new PDFImage(pdfPath, {graphicsMagick: true}); | ||
expect(pdfImage.constructConvertCommandForPage(1)) | ||
.equal("gm convert '/tmp/test.pdf[1]' '/tmp/test-1.png'"); | ||
.equal('gm convert "/tmp/test.pdf[1]" "/tmp/test-1.png"'); | ||
}); | ||
@@ -45,23 +68,80 @@ | ||
it("should convert PDF's page to a file with the default extension", function () { | ||
pdfImage.convertPage(1).then(function (imagePath) { | ||
expect(imagePath).equal("/tmp/test-1.png"); | ||
expect(fs.existsSync("/tmp/test-1.png")).to.be.true; | ||
return new Promise(function(resolve, reject) { | ||
pdfImage.convertPage(1).then(function (imagePath) { | ||
expect(imagePath).equal("/tmp/test-1.png"); | ||
expect(fs.existsSync(imagePath)).to.be.true; | ||
generatedFiles.push(imagePath); | ||
resolve(); | ||
}).catch(function(err){ | ||
reject(err); | ||
}); | ||
}); | ||
pdfImage.convertPage(10).then(function (imagePath) { | ||
expect(imagePath).equal("/tmp/test-10.png"); | ||
expect(fs.existsSync("/tmp/test-10.png")).to.be.true; | ||
}); | ||
}); | ||
it("should convert PDF's page 10 to a file with the default extension", function () { | ||
return new Promise(function(resolve, reject){ | ||
pdfImage.convertPage(9).then(function (imagePath) { | ||
expect(imagePath).equal("/tmp/test-9.png"); | ||
expect(fs.existsSync(imagePath)).to.be.true; | ||
generatedFiles.push(imagePath); | ||
resolve(); | ||
}).catch(function(err){ | ||
reject(err); | ||
}); | ||
}) | ||
}); | ||
it("should convert PDF's page to file with a specified extension", function () { | ||
pdfImage.setConvertExtension("jpeg"); | ||
pdfImage.convertPage(1).then(function (imagePath) { | ||
expect(imagePath).equal("/tmp/test-1.jpeg"); | ||
expect(fs.existsSync("/tmp/test-1.jpeg")).to.be.true; | ||
return new Promise(function(resolve, reject) { | ||
pdfImage.setConvertExtension("jpeg"); | ||
pdfImage.convertPage(1).then(function (imagePath) { | ||
expect(imagePath).equal("/tmp/test-1.jpeg"); | ||
expect(fs.existsSync(imagePath)).to.be.true; | ||
generatedFiles.push(imagePath); | ||
resolve(); | ||
}).catch(function(err){ | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
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 () { | ||
pdfImage.numberOfPages().then(function (numberOfPages) { | ||
expect(numberOfPages).to.be.equal(21); | ||
return new Promise(function(resolve, reject) { | ||
pdfImage.numberOfPages().then(function (numberOfPages) { | ||
expect(parseInt(numberOfPages)).to.be.equal(10); | ||
resolve(); | ||
}).catch(function(err){ | ||
reject(err); | ||
}); | ||
}); | ||
@@ -77,2 +157,32 @@ }); | ||
}); | ||
afterEach(function(done){ | ||
//cleanUp files generated during test | ||
let i = generatedFiles.length; | ||
if (i > 0 ){ | ||
generatedFiles.forEach(function(filepath, index){ | ||
fs.unlink(filepath, function(err) { | ||
i--; | ||
if (err) { | ||
done(err); | ||
} else if (i <= 0) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
generatedFiles = []; //clear after delete | ||
} else { | ||
done(); | ||
} | ||
}); | ||
after(function(done){ | ||
//finaly - remove test.pdf from /tmp/ | ||
fs.unlink(pdfPath, function(err) { | ||
if (err) { | ||
done(err); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
33604
1
6
0
371
90
2
+ Addedes6-promise@4.2.8(transitive)
- Removedchai@~1.9.2
- Removedassertion-error@1.0.0(transitive)
- Removedchai@1.9.2(transitive)
- Removeddeep-eql@0.1.3(transitive)
- Removedes6-promise@2.0.1(transitive)
- Removedtype-detect@0.1.1(transitive)
Updatedes6-promise@~4.2.4