我正在尝试将Ghostscript命令放入循环中,以便能够处理十页的大pdf文件。我似乎无法正确解决。每个Ghostscript命令应在下一个命令开始之前完成。
//first I create chunks (arrays of 10 items) from my original array
const pageChunks = arr.chunk(pdfData.formImage.Pages, 10);
const pagePromises = [];
let processScript = async (index, chunk) => {
await new Promise((resolve, reject) => {
gs()
.batch()
.nopause()
.q()
.device('jpeg')
.executablePath('lambda-ghostscript/bin/./gs')
.option('-dFirstPage=' + ((index * 10) + 1))
.option('-dLastPage=' + ((index * 10) + Math.min(chunk.length, 10)))
.res(pdfRes)
.output(path.join(jpegPagesFolder, identifier + fileSfx))
.input(preparedPdf)
.exec(async (err, stdout, stderr) => {
if (err) {
err.originalMessage = err && err.message ? err.message : 'Unknown error';
err.message = 'Error running Ghostscript';
err.bucket = bucketName;
err.identifier = identifier;
return response.sendError(res, 400, 'Error running Ghostscript')(err);
} else {
log.debug('Ghostscript chunk ' + (index + 1) + ' completed successfully');
}
})
});
};
await pageChunks.map((chunk, index) => {
pagePromises.push( processScript(index, chunk));
});
let chain = Promise.resolve();
for (const func of pagePromises) {
chain = chain.then(func);
}
我也尝试了其他几种方法,例如
Promise.all(pagePromises)
但是没有雪茄。所有Ghostscripts命令都会彼此立即被触发,而不是彼此等待,从而弄乱了输出文件。