Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pdf-image

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pdf-image - npm Package Compare versions

Comparing version 0.0.1 to 1.0.1

90

index.js

@@ -8,6 +8,12 @@ // node-pdf

var util = require("util");
var exec = require("child_process").exec;
function PDFImage(pdfFilePath) {
function PDFImage(pdfFilePath, options) {
if (!options) options = {};
this.pdfFilePath = pdfFilePath;
this.pdfFileBaseName = path.basename(pdfFilePath, ".pdf");
this.setConvertOptions(options.convertOptions || {});
// TODO: make out dir customizable

@@ -18,2 +24,40 @@ this.outputDirectory = path.dirname(pdfFilePath);

PDFImage.prototype = {
constructGetInfoCommand: function () {
return util.format(
"pdfinfo '%s'",
this.pdfFilePath
);
},
parseGetInfoCommandOutput: function (output) {
var info = {};
output.split("\n").forEach(function (line) {
if (line.match(/^(.*?):[ \t]*(.*)$/)) {
info[RegExp.$1] = RegExp.$2;
}
});
return info;
},
getInfo: function () {
var self = this;
var getInfoCommand = this.constructGetInfoCommand();
var promise = new Promise(function (resolve, reject) {
exec(getInfoCommand, function (err, stdout, stderr) {
if (err) {
return reject({
message: "Failed to get PDF'S information",
error: err,
stdout: stdout,
stderr: stderr
});
}
return resolve(self.parseGetInfoCommandOutput(stdout));
});
});
return promise;
},
numberOfPages: function () {
return this.getInfo().then(function (info) {
return info["Pages"];
});
},
getOutputImagePathForPage: function (pageNumber) {

@@ -25,20 +69,40 @@ return path.join(

},
getConvertCommandForPage: function (pageNumber) {
setConvertOptions: function (convertOptions) {
this.convertOptions = convertOptions || {};
},
constructConvertCommandForPage: function (pageNumber) {
var pdfFilePath = this.pdfFilePath;
var outputImagePath = this.getOutputImagePathForPage(pageNumber);
var convertOptionString = this.constructConvertOptions();
return util.format(
"convert '%s[%d]' %s",
"convert %s'%s[%d]' %s",
convertOptionString ? convertOptionString + " " : "",
pdfFilePath, pageNumber, outputImagePath
);
},
constructConvertOptions: function () {
return Object.keys(this.convertOptions).sort().map(function (optionName) {
if (this.convertOptions[optionName] !== null) {
return optionName + " " + this.convertOptions[optionName];
} else {
return optionName;
}
}, this).join(" ");
},
convertPage: function (pageNumber) {
var pdfFilePath = this.pdfFilePath;
var outputImagePath = this.getOutputImagePathForPage(pageNumber);
var convertCommand = this.getConvertCommandForPage(pageNumber);
var convertCommand = this.constructConvertCommandForPage(pageNumber);
var promise = new Promise(function (resolve, reject) {
function convertPageToImage() {
var exec = require("child_process").exec;
exec(convertCommand, function (err, stdout, stderr) {
if (err) { return reject(err); }
if (err) {
return reject({
message: "Failed to convert page to image",
error: err,
stdout: stdout,
stderr: stderr
});
}
return resolve(outputImagePath);

@@ -50,3 +114,8 @@ });

var imageNotExists = err && err.code === "ENOENT";
if (!imageNotExists && err) { return reject(err); }
if (!imageNotExists && err) {
return reject({
message: "Failed to stat image file",
error: err
});
}

@@ -64,3 +133,8 @@ // convert when (1) image doesn't exits or (2) image exists

fs.stat(pdfFilePath, function (err, pdfFileStat) {
if (err) { return reject(err); }
if (err) {
return reject({
message: "Failed to stat PDF file",
error: err
});
}

@@ -67,0 +141,0 @@ if (imageFileStat.mtime < pdfFileStat.mtime) {

{
"name": "pdf-image",
"version": "0.0.1",
"version": "1.0.1",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/mooz/node-pdf-image.git"
},
"bugs": {
"url": "https://github.com/mooz/node-pdf-image/issues"
},
"homepage": "https://github.com/mooz/node-pdf-image#readme",
"dependencies": {
"es6-promise": "~2.0.0"
"es6-promise": "~2.0.0",
"chai": "~1.9.2"
}
}

4

README.md

@@ -10,7 +10,7 @@ # pdf-image

Ensure you have `convert` and `gs` commands.
Ensure you have `convert`, `gs`, and `pdfinfo` (part of poppler) commands.
### Ubuntu
sudo apt-get install imagemagick ghostscript
sudo apt-get install imagemagick ghostscript poppler-utils

@@ -17,0 +17,0 @@ ## Usage

@@ -12,3 +12,3 @@ var assert = require("assert");

it("should has correct basename", function () {
it("should have correct basename", function () {
expect(pdfImage.pdfFileBaseName).equal("test");

@@ -27,3 +27,3 @@ });

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

@@ -43,2 +43,17 @@ });

});
it("should return # of pages", function () {
pdfImage.numberOfPages().then(function (numberOfPages) {
expect(numberOfPages).to.be.equal(21);
});
});
it("should construct convert options correctly", function () {
pdfImage.setConvertOptions({
"-density": 300,
"-trim": null
});
expect(pdfImage.constructConvertOptions()).equal("-density 300 -trim");
pdfImage.setConvertOptions(null);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc