Switch to async spawn method to avoid filling up stdout buffer.
This commit is contained in:
parent
24f22fce01
commit
1bc218b26d
1 changed files with 93 additions and 36 deletions
129
server/server.js
129
server/server.js
|
|
@ -463,7 +463,57 @@ function svg2pdf(svg_filename) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function combine_pdfs(pages, filename) {
|
function spawn(cmd, args, options) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let stdin;
|
||||||
|
if (options && options.input) {
|
||||||
|
stdin = options.input;
|
||||||
|
delete options.input;
|
||||||
|
}
|
||||||
|
|
||||||
|
const proc = child_process.spawn(cmd, args, options);
|
||||||
|
if (stdin) {
|
||||||
|
proc.stdin.write(stdin, (error) => {
|
||||||
|
proc.stdin.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let data;
|
||||||
|
let errorData;
|
||||||
|
|
||||||
|
proc.on("message", console.log);
|
||||||
|
|
||||||
|
proc.stdout.on("data", chunk => {
|
||||||
|
if (!data) {
|
||||||
|
data = chunk;
|
||||||
|
} else {
|
||||||
|
data = Buffer.concat([data, chunk]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
proc.stderr.on("data", chunk => {
|
||||||
|
if (!errorData) {
|
||||||
|
errorData = chunk;
|
||||||
|
} else {
|
||||||
|
errorData = Buffer.concat([errorData, chunk]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
proc.on("close", function(code) {
|
||||||
|
resolve({
|
||||||
|
"status": code,
|
||||||
|
"stdout": data,
|
||||||
|
"stderr": errorData,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
proc.on("error", function(err) {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function combine_pdfs(pages, filename) {
|
||||||
if (!pages.length) {
|
if (!pages.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -474,7 +524,7 @@ function combine_pdfs(pages, filename) {
|
||||||
args.push("cat");
|
args.push("cat");
|
||||||
args.push("output");
|
args.push("output");
|
||||||
args.push(filename);
|
args.push(filename);
|
||||||
var status = child_process.spawnSync(cmd, args);
|
var status = await spawn(cmd, args);
|
||||||
console.log("Done", status.status);
|
console.log("Done", status.status);
|
||||||
if (status.status !== 0) {
|
if (status.status !== 0) {
|
||||||
if (status.stderr) {
|
if (status.stderr) {
|
||||||
|
|
@ -489,7 +539,7 @@ function combine_pdfs(pages, filename) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function overlay_pdf(source_pdf, overlay_filename) {
|
async function overlay_pdf(source_pdf, overlay_filename) {
|
||||||
if (!overlay_filename) {
|
if (!overlay_filename) {
|
||||||
return source_pdf;
|
return source_pdf;
|
||||||
}
|
}
|
||||||
|
|
@ -497,7 +547,7 @@ function overlay_pdf(source_pdf, overlay_filename) {
|
||||||
var cmd = CMD_PDFTK;
|
var cmd = CMD_PDFTK;
|
||||||
console.log("Merging combined with overlay ...");
|
console.log("Merging combined with overlay ...");
|
||||||
var args = ["-", "multistamp", overlay_filename, "output", "-"];
|
var args = ["-", "multistamp", overlay_filename, "output", "-"];
|
||||||
var status = child_process.spawnSync(cmd, args, {
|
var status = await spawn(cmd, args, {
|
||||||
"input": source_pdf,
|
"input": source_pdf,
|
||||||
});
|
});
|
||||||
console.log("Done", status.status);
|
console.log("Done", status.status);
|
||||||
|
|
@ -518,7 +568,7 @@ function overlay_pdf(source_pdf, overlay_filename) {
|
||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_text_annotations(source_pdf, text, tempdir) {
|
async function add_text_annotations(source_pdf, text, tempdir) {
|
||||||
if (!text || !text.length) {
|
if (!text || !text.length) {
|
||||||
return source_pdf;
|
return source_pdf;
|
||||||
}
|
}
|
||||||
|
|
@ -530,7 +580,7 @@ function add_text_annotations(source_pdf, text, tempdir) {
|
||||||
fs.writeFileSync(text_filename, JSON.stringify(text));
|
fs.writeFileSync(text_filename, JSON.stringify(text));
|
||||||
|
|
||||||
var args = ["--text", text_filename, "-", "-"];
|
var args = ["--text", text_filename, "-", "-"];
|
||||||
var status = child_process.spawnSync(cmd, args, {
|
var status = await spawn(cmd, args, {
|
||||||
"input": source_pdf,
|
"input": source_pdf,
|
||||||
});
|
});
|
||||||
console.log("Done", status.status);
|
console.log("Done", status.status);
|
||||||
|
|
@ -656,11 +706,14 @@ var runSvg2PdfCombiner = function(tempdir, data) {
|
||||||
Promise.all(promises)
|
Promise.all(promises)
|
||||||
.then(function(pdfs) {
|
.then(function(pdfs) {
|
||||||
var combined_filename = tempdir + "/combined.pdf";
|
var combined_filename = tempdir + "/combined.pdf";
|
||||||
if (!combine_pdfs(pdfs, combined_filename)) {
|
combine_pdfs(pdfs, combined_filename)
|
||||||
reject();
|
.then((status) => {
|
||||||
return;
|
if (!status) {
|
||||||
}
|
reject();
|
||||||
resolve(combined_filename);
|
return;
|
||||||
|
}
|
||||||
|
resolve(combined_filename);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
|
@ -809,33 +862,37 @@ var server = http.createServer(function(request, response) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var merged = overlay_pdf(source_pdf, combined_filename);
|
overlay_pdf(source_pdf, combined_filename)
|
||||||
merged = add_text_annotations(merged, data.text, tempdir);
|
.then((merged) => {
|
||||||
deleteFolderRecursive(tempdir);
|
add_text_annotations(merged, data.text, tempdir)
|
||||||
if (!merged) {
|
.then((merged) => {
|
||||||
response.writeHead(500, {
|
deleteFolderRecursive(tempdir);
|
||||||
"Access-Control-Allow-Origin": "*",
|
if (!merged) {
|
||||||
"Access-Control-Allow-Methods": "POST",
|
response.writeHead(500, {
|
||||||
"Access-Control-Allow-Headers": "Content-Type, Content-Disposition",
|
"Access-Control-Allow-Origin": "*",
|
||||||
"Access-Control-Expose-Headers": "Content-Disposition",
|
"Access-Control-Allow-Methods": "POST",
|
||||||
"Content-Type": "text/plain"
|
"Access-Control-Allow-Headers": "Content-Type, Content-Disposition",
|
||||||
});
|
"Access-Control-Expose-Headers": "Content-Disposition",
|
||||||
response.end('Internal Server Error');
|
"Content-Type": "text/plain"
|
||||||
return;
|
});
|
||||||
}
|
response.end('Internal Server Error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
var filename = getDownloadFilename(decoded.filename || room_id, now);
|
var filename = getDownloadFilename(decoded.filename || room_id, now);
|
||||||
response.writeHead(200, {
|
response.writeHead(200, {
|
||||||
"Access-Control-Allow-Origin": "*",
|
"Access-Control-Allow-Origin": "*",
|
||||||
"Access-Control-Allow-Methods": "POST",
|
"Access-Control-Allow-Methods": "POST",
|
||||||
"Access-Control-Allow-Headers": "Content-Type, Content-Disposition",
|
"Access-Control-Allow-Headers": "Content-Type, Content-Disposition",
|
||||||
"Access-Control-Expose-Headers": "Content-Disposition",
|
"Access-Control-Expose-Headers": "Content-Disposition",
|
||||||
"Content-Type": "application/pdf",
|
"Content-Type": "application/pdf",
|
||||||
"Content-Disposition": "attachment; filename=\"" + filename + "\"",
|
"Content-Disposition": "attachment; filename=\"" + filename + "\"",
|
||||||
"Content-Length": merged.length
|
"Content-Length": merged.length
|
||||||
|
});
|
||||||
|
response.end(merged);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
response.end(merged);
|
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
deleteFolderRecursive(tempdir);
|
deleteFolderRecursive(tempdir);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue