pdf2img-lambda-friendly
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -1,3 +0,3 @@ | ||
"use strict"; | ||
'use strict' | ||
module.exports = require('./lib/pdf2img'); | ||
module.exports = require('./lib/pdf2img') |
@@ -1,10 +0,9 @@ | ||
"use strict"; | ||
'use strict' | ||
var fs = require('fs'); | ||
var fs = require('fs') | ||
var gm = require('gm').subClass({ | ||
imageMagick: true | ||
}); | ||
var gs = require('node-gs'); | ||
var path = require('path'); | ||
var async = require('async'); | ||
imageMagick: true | ||
}) | ||
var gs = require('node-gs') | ||
var path = require('path') | ||
@@ -18,22 +17,22 @@ var options = { | ||
page: null | ||
}; | ||
} | ||
var Pdf2Img = function() {}; | ||
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.page = opts.page || options.page; | ||
}; | ||
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.page = opts.page || options.page | ||
} | ||
Pdf2Img.prototype.convert = function(input, callbackreturn) { | ||
Pdf2Img.prototype.convert = function (input, callbackreturn) { | ||
// Make sure it has correct extension | ||
if (path.extname(path.basename(input)) != '.pdf') { | ||
if (path.extname(path.basename(input)) !== '.pdf') { | ||
return callbackreturn({ | ||
result: 'error', | ||
message: 'Unsupported file type.' | ||
}); | ||
}) | ||
} | ||
@@ -46,13 +45,12 @@ | ||
message: 'Input file not found.' | ||
}); | ||
}) | ||
} | ||
var stdout = []; | ||
var output = path.basename(input, path.extname(path.basename(input))); | ||
var output = path.basename(input, path.extname(path.basename(input))) | ||
// Set output dir | ||
if (options.outputdir) { | ||
options.outputdir = options.outputdir + path.sep; | ||
options.outputdir = options.outputdir + path.sep | ||
} else { | ||
options.outputdir = output + path.sep; | ||
options.outputdir = output + path.sep | ||
} | ||
@@ -62,3 +60,3 @@ | ||
if (!isDirExists(options.outputdir)) { | ||
fs.mkdirSync(options.outputdir); | ||
fs.mkdirSync(options.outputdir) | ||
} | ||
@@ -68,127 +66,105 @@ | ||
if (options.outputname) { | ||
options.outputname = options.outputname; | ||
options.outputname = options.outputname | ||
} else { | ||
options.outputname = output; | ||
options.outputname = output | ||
} | ||
async.waterfall([ | ||
// Get pages count | ||
function (callback) { | ||
gm(input).identify("%p ", function (err, value) { | ||
var pageCount = String(value).split(' '); | ||
if (!pageCount.length) { | ||
callback({ | ||
result: 'error', | ||
message: 'Invalid page number.' | ||
}, null); | ||
} else { | ||
// Convert selected page | ||
if (options.page !== null) { | ||
if (options.page < pageCount.length) { | ||
callback(null, [options.page]); | ||
} else { | ||
callback({ | ||
result: 'error', | ||
message: 'Invalid page number.' | ||
}, null); | ||
} | ||
} else { | ||
callback(null, pageCount); | ||
} | ||
} | ||
gm(input).identify('%p ', function (err, value) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
const pageCount = String(value).split(' ').length | ||
if (options.page > pageCount) { | ||
throw new Error('Incorrect page number in options') | ||
} | ||
if (pageCount === 0) { | ||
throw new Error('Page number is 0') | ||
} | ||
let promises = [] | ||
}) | ||
const convertOnePage = (pageNum, inputData) => { | ||
return new Promise((resolve, reject) => { | ||
var inputStream = fs.createReadStream(inputData) | ||
var outputFile = options.outputdir + options.outputname + '_' + pageNum + '.' + options.type | ||
}, | ||
// Convert pdf file | ||
function(pages, callback) { | ||
// Use eachSeries to make sure that conversion has done page by page | ||
async.eachSeries(pages, function(page, callbackmap) { | ||
var inputStream = fs.createReadStream(input); | ||
page = parseInt(page) + 1 | ||
var outputFile = options.outputdir + options.outputname + '_' + page + '.' + options.type; | ||
convertPdf2Img(inputStream, outputFile, page, function(error, result) { | ||
return convertPdf2Img(inputStream, outputFile, pageNum, function (error, result) { | ||
if (error) { | ||
return callbackmap(error); | ||
console.log(error) | ||
return reject(error) | ||
} | ||
stdout.push(result); | ||
return callbackmap(error, result); | ||
}); | ||
}, function(e) { | ||
if (e) callback(e); | ||
return callback(null, { | ||
return resolve(result) | ||
}) | ||
}) | ||
} | ||
for (let j = 0; j < pageCount; j++) { | ||
promises.push(convertOnePage(j + 1, input)) | ||
} | ||
return Promise.all(promises) | ||
.then(result => { | ||
callbackreturn(null, { | ||
result: 'success', | ||
message: stdout | ||
}); | ||
}); | ||
} | ||
], callbackreturn); | ||
}; | ||
message: result | ||
}) | ||
}) | ||
.catch(e => console.log(e)) | ||
}) | ||
} | ||
var convertPdf2Img = function (input, output, page, callback) { | ||
if (input.path) { | ||
var filepath = input.path; | ||
} else { | ||
return callback({ | ||
result: 'error', | ||
message: 'Invalid input file path.' | ||
}, null); | ||
} | ||
console.log(input); | ||
console.log(output); | ||
console.log(page) | ||
console.log(filepath) | ||
if (input.path) { | ||
var filepath = input.path | ||
} else { | ||
let err = {result: 'error', message: 'Invalid input file path.'} | ||
return callback(err, null) | ||
} | ||
gs() | ||
.batch() | ||
.nopause() | ||
.option('-r' + options.density) | ||
// .option('-dDownScaleFactor=2') | ||
.option('-dFirstPage=' + page) | ||
.option('-dLastPage=' + page) | ||
.executablePath('lambda-ghostscript/bin/./gs') | ||
.device('png16m') | ||
.output(output) | ||
.input(filepath) | ||
.exec(function (err, stdout, stderr) { | ||
console.log(stderr); | ||
if (err) { | ||
return callback({ | ||
result: 'error', | ||
message: err | ||
}, null); | ||
} | ||
try { | ||
if (!(fs.statSync(output)['size'] / 1000)) { | ||
return callback({ | ||
result: 'error', | ||
message: 'Zero sized output image detected.' | ||
}, null); | ||
} | ||
gs() | ||
.batch() | ||
.nopause() | ||
.option('-r' + options.density) | ||
// .option('-dDownScaleFactor=2') | ||
.option('-dFirstPage=' + page) | ||
.option('-dLastPage=' + page) | ||
.executablePath('lambda-ghostscript/bin/./gs') | ||
.device('png16m') | ||
.output(output) | ||
.input(filepath) | ||
.exec(function (err, stdout, stderr) { | ||
if (err) { | ||
err = { | ||
result: 'error', | ||
message: err | ||
} | ||
return callback(err, null) | ||
} | ||
try { | ||
if (!(fs.statSync(output)['size'] / 1000)) { | ||
let err = { | ||
result: 'error', | ||
message: 'Zero sized output image detected.' | ||
} | ||
return callback(err, null) | ||
} | ||
var results = { | ||
page: page, | ||
name: path.basename(output), | ||
size: fs.statSync(output)['size'] / 1000.0, | ||
path: output | ||
}; | ||
var results = { | ||
page: page, | ||
name: path.basename(output), | ||
size: fs.statSync(output)['size'] / 1000.0, | ||
path: output | ||
} | ||
return callback(null, results); | ||
} catch (e) { | ||
return callback(e); | ||
} | ||
}); | ||
}; | ||
return callback(null, results) | ||
} catch (e) { | ||
return callback(e) | ||
} | ||
}) | ||
} | ||
// Check if directory is exists | ||
var isDirExists = function(path) { | ||
var isDirExists = function (path) { | ||
try { | ||
return fs.statSync(path).isDirectory(); | ||
return fs.statSync(path).isDirectory() | ||
} catch (e) { | ||
return false; | ||
return false | ||
} | ||
@@ -198,10 +174,10 @@ } | ||
// Check if file is exists | ||
var isFileExists = function(path) { | ||
var isFileExists = function (path) { | ||
try { | ||
return fs.statSync(path).isFile(); | ||
return fs.statSync(path).isFile() | ||
} catch (e) { | ||
return false; | ||
return false | ||
} | ||
} | ||
module.exports = new Pdf2Img; | ||
module.exports = new Pdf2Img() |
{ | ||
"name": "pdf2img-lambda-friendly", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A nodejs module for converting pdf into image", | ||
@@ -13,3 +13,2 @@ "author": "Fitra Aditya <fitra@g.pl>", | ||
"dependencies": { | ||
"async": "^0.9.0", | ||
"gm": "^1.17.0", | ||
@@ -16,0 +15,0 @@ "node-gs": "^0.1.1" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33452
2
9
427
3
1
- Removedasync@^0.9.0
- Removedasync@0.9.2(transitive)