Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 46 additions & 36 deletions lib/pdf2png.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [];
Expand All @@ -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++)
});
Expand Down