Switch to async spawn method to avoid filling up stdout buffer.

This commit is contained in:
Joachim Bauch 2021-11-11 11:54:58 +01:00
parent 24f22fce01
commit 1bc218b26d
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02

View file

@ -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)
.then((status) => {
if (!status) {
reject(); reject();
return; return;
} }
resolve(combined_filename); resolve(combined_filename);
});
}) })
.catch(function(error) { .catch(function(error) {
reject(error); reject(error);
@ -809,8 +862,10 @@ 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) => {
add_text_annotations(merged, data.text, tempdir)
.then((merged) => {
deleteFolderRecursive(tempdir); deleteFolderRecursive(tempdir);
if (!merged) { if (!merged) {
response.writeHead(500, { response.writeHead(500, {
@ -836,6 +891,8 @@ var server = http.createServer(function(request, response) {
"Content-Length": merged.length "Content-Length": merged.length
}); });
response.end(merged); response.end(merged);
});
});
}) })
.catch(function(error) { .catch(function(error) {
deleteFolderRecursive(tempdir); deleteFolderRecursive(tempdir);