我正在使用fill-pdf npm模块来填充模板pdf,它创建了新文件,该文件从磁盘读取并作为缓冲区返回回调。我有两个文件,我对其执行相同的操作。我想在那里合并两个缓冲区,以形成一个单独的pdf文件,我可以将其发送回客户端。我尝试了不同的缓冲区串联方法。可以使用Buffer.concat来连接缓冲区,例如,
var newBuffer = Buffer.concat([result_pdf.output, result_pdf_new.output]);
新缓冲区的大小也是输入缓冲区的大小之和。但是,当仍然newBuffer将_作为响应发送给客户端时,它仅显示数组中最后提到的文件。
newBuffer
res.type("application/pdf"); return res.send(buffer);
任何想法 ?
HummusJS支持使用其appendPDFPagesFromPDF方法合并PDF
使用流与缓冲区一起使用的示例:
const hummus = require('hummus'); const memoryStreams = require('memory-streams'); /** * Concatenate two PDFs in Buffers * @param {Buffer} firstBuffer * @param {Buffer} secondBuffer * @returns {Buffer} - a Buffer containing the concactenated PDFs */ const combinePDFBuffers = (firstBuffer, secondBuffer) => { var outStream = new memoryStreams.WritableStream(); try { var firstPDFStream = new hummus.PDFRStreamForBuffer(firstBuffer); var secondPDFStream = new hummus.PDFRStreamForBuffer(secondBuffer); var pdfWriter = hummus.createWriterToModify(firstPDFStream, new hummus.PDFStreamForResponse(outStream)); pdfWriter.appendPDFPagesFromPDF(secondPDFStream); pdfWriter.end(); var newBuffer = outStream.toBuffer(); outStream.end(); return newBuffer; } catch(e){ outStream.end(); throw new Error('Error during PDF combination: ' + e.message); } }; combinePDFBuffers(PDFBuffer1, PDFBuffer2);