From 4acaeaafad986b2d0f4601c79d1a8d4638e2aa56 Mon Sep 17 00:00:00 2001 From: salim Date: Thu, 27 Dec 2018 23:55:55 +0100 Subject: [PATCH] Bug fixing TypeError [ERR_INVALID_CALLBACK]: Callback must be a function for Nodejs version 8.1 and above --- lib/pdf2png.js | 82 ++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/pdf2png.js b/lib/pdf2png.js index cb01051..106ec50 100644 --- a/lib/pdf2png.js +++ b/lib/pdf2png.js @@ -18,9 +18,8 @@ exports.ghostscriptPath = projectPath + "\\executables\\ghostScript"; exports.ghostscriptPath = exports.ghostscriptPath.split("\\").join("/"); function getPageCount(callback, filepathOrData) { - pdfPageCount.count(filepathOrData, function(resp2){ - if(!resp2.success) - { + pdfPageCount.count(filepathOrData, function (resp2) { + if (!resp2.success) { console.log("Something went wrong: " + resp2.error); return; @@ -30,71 +29,78 @@ function getPageCount(callback, filepathOrData) { }; -function getImage(callback, options, imageFilepath, resp, i){ - exec("gs -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r" + options.quality + " -dFirstPage="+i+" -dLastPage="+i+" -sOutputFile=" + imageFilepath + " " + '"' + resp.data + '"', function (error, stdout, stderr) { +function getImage(callback, options, imageFilepath, resp, i) { + exec("gs -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r" + options.quality + " -dFirstPage=" + i + " -dLastPage=" + i + " -sOutputFile=" + imageFilepath + " " + '"' + resp.data + '"', function (error, stdout, stderr) { // Remove temp files resp.clean(); - if(error !== null) - { - callback({ success: false, error: "Error converting pdf to png: " + error }); + if (error !== null) { + callback({ + success: false, + error: "Error converting pdf to png: " + error + }); return; } - if(options.returnFilePath) - { - callback({ success: true, data: imageFilepath }); + if (options.returnFilePath) { + callback({ + success: true, + data: imageFilepath + }); return; } var img = fs.readFileSync(imageFilepath); // Remove temp file - fs.unlink(imageFilepath); - - callback({ success: true, data: img, number: i }); + fs.unlink(imageFilepath, (err) => { + if (err != null) + return; + }); + + callback({ + success: true, + data: img, + number: i + }); }); } -exports.convert = function() { +exports.convert = function () { var filepathOrData = arguments[0]; var callback = arguments[1]; var options = {}; - + var tmpFileCreated = false; - if(arguments[2] != null) - { + if (arguments[2] != null) { options = arguments[1]; callback = arguments[2]; } - - if(!initialized) - { - if(!options.useLocalGhostscript) - { + + if (!initialized) { + if (!options.useLocalGhostscript) { process.env.Path += ";" + exports.ghostscriptPath; } - + initialized = true; } options.quality = options.quality || 100; - - filesource.getDataPath(filepathOrData, function(resp){ - if(!resp.success) - { + + filesource.getDataPath(filepathOrData, function (resp) { + if (!resp.success) { callback(resp); return; } - getPageCount(function(resp2) { + getPageCount(function (resp2) { // get temporary filepath - for(var i = 1; i <= resp2.data; i++){ + for (var i = 1; i <= resp2.data; i++) { var result = new Object(); result.data = []; @@ -104,20 +110,24 @@ exports.convert = function() { var her = 1; foo = resp2.data; - tmp.file({ postfix: ".png" }, function(err, imageFilepath, fd) { + tmp.file({ + postfix: ".png" + }, function (err, imageFilepath, fd) { - if(err) - { - callback({ success: false, error: "Error getting second temporary filepath: " + err }); + if (err) { + callback({ + success: false, + error: "Error getting second temporary filepath: " + err + }); return; } - getImage(function(resp3){ + getImage(function (resp3) { //result.data.push(resp3.data); result.data[resp3.number] = resp3.data; result.success = resp3.success; bar++; - if(foo == bar) + if (foo == bar) callback(result) }, options, imageFilepath, resp, her++) });