Nodejs: combining streams of PDF buffers

pdf-merge provides an api for merging PDF files, but it does not work when trying to combine buffers or streams. When trying to combine these buffers in a standard way or through packets (aka Buffer.concat, stream-concat, buffer-concat), the result includes only the last stream instead of the combined result.

It seems that more people encountered this problem, but no solution was provided (example: NodeJS: merge two PDF files into one using the buffer obtained by reading them )

I assume this is due to the unique presentation of the PDF file.

I also tried pdfkit addContent (buffer), but the result is an empty file and apparently it is not supported ( https://github.com/devongovett/pdfkit/issues/417 ).

Has anyone experienced this problem and made it work? We should use threads for performance problems (therefore, file mitigation is not an option).

Thank.

+4
source share
2 answers

I found node-pdftkit very useful for this. By simply passing an array of buffers as input, and then turning it into immediately output, you should get a combined set of PDF files.

npm i node-pdftk

const pdfs = [...] // array of PDF buffers

pdftk
  .input(pdfs)
  .output()
  .then(buf => {
    res.type('application/pdf');
    res.send(buf);
  });
+2
source

Google App, , .

0

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


All Articles