NodeJS: merge two PDF files into one using the buffer obtained by reading them

I use the fill-pdf npm module to populate the pdf template and it creates a new file that is read from disk and returned as a buffer for the callback. I have two files for which I perform the same operation. I want to combine the two buffers there to form a single PDF file that I can send back to the client. I have tried various buffer concatenation methods. A buffer can be combined using Buffer.concat, for example,

var newBuffer = Buffer.concat([result_pdf.output, result_pdf_new.output]); 

The size of the new buffer is also the sum of the size of the input buffers. But still, when newBuffer sent to the client as an answer, it only shows the file specified by the last in the array.

 res.type("application/pdf"); return res.send(buffer); 

Any idea?

+9
source share
2 answers

HummusJS supports merging PDF files using the appendPDFPagesFromPDF method

An example of using streams to work with buffers:

 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); 
+10
source

Here's what we use on our Express server to aggregate a list of PDF blobs.

 const { PDFRStreamForBuffer, createWriterToModify, PDFStreamForResponse } = require('hummus'); const { WritableStream } = require('memory-streams'); // Merge the pages of the pdfBlobs (Javascript buffers) into a single PDF blob const mergePdfs = pdfBlobs => { if (pdfBlobs.length === 0) throw new Error('mergePdfs called with empty list of PDF blobs'); // This optimization is not necessary, but it avoids the churn down below if (pdfBlobs.length === 1) return pdfBlobs[0]; // Adapted from: https://stackoverflow.com/questions/36766234/nodejs-merge-two-pdf-files-into-one-using-the-buffer-obtained-by-reading-them?answertab=active#tab-top // Hummus is useful, but with poor interfaces -- Eg createWriterToModify shouldn't require any PDF stream // And Hummus has many Issues: https://github.com/galkahana/HummusJS/issues const [firstPdfRStream, ...restPdfRStreams] = pdfBlobs.map(pdfBlob => new PDFRStreamForBuffer(pdfBlob)); const outStream = new WritableStream(); const pdfWriter = createWriterToModify(firstPdfRStream, new PDFStreamForResponse(outStream)); restPdfRStreams.forEach(pdfRStream => pdfWriter.appendPDFPagesFromPDF(pdfRStream)); pdfWriter.end(); outStream.end(); return outStream.toBuffer(); }; module.exports = exports = { mergePdfs, }; 
0
source

Source: https://habr.com/ru/post/1247602/


All Articles