Combining PDFs in Node

Hi, I am trying to combine pdf for a total of n, but I cannot get it to work.

I use a buffer module to compress pdf, but it will only apply the last pdf file to the final pdf.

Is it possible to do this in node?

var pdf1 = fs.readFileSync('./test1.pdf'); var pdf2 = fs.readFileSync('./test2.pdf'); fs.writeFile("./final_pdf.pdf", Buffer.concat([pdf1, pdf2]), function(err) { if(err) { return console.log(err); } console.log("The file was saved!"); }); 

Some libraries currently exist, but they all depend on other software or programming languages.

+3
source share
2 answers

What do you expect to get when you do Buffer.concat([pdf1, pdf2]) ? Just combining two PDF files, you will not get one containing all the pages. PDF is a complex format (mainly for vector graphics). If you just added two JPEG files, you would not expect to get a large image containing both pictures, would you?

You will need to use an external library. https://github.com/wubzz/pdf-merge can work, for example.

+11
source

HummusJS is another library for working with PDF, but without dependence on PDFtk. See this answer for an example of combining PDF files in buffers.

+5
source

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


All Articles