mertje
mertje

Reputation: 486

Merging PDFs in Node

Hi i'm trying to merge pdf's of total of n but I cannot get it to work.

I'm using the Buffer module to concat the pdf's but it does only apply the last pdf in to the final pdf.

Is this even possible thing to do 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!");
});

There are currently some libs out there but they do all depend on either other software or programming languages.

Upvotes: 8

Views: 23514

Answers (3)

Tilal Ahmad
Tilal Ahmad

Reputation: 939

Aspose.PDF Cloud SDK for Node.js can merge/combine pdf documents without depending on any third-party API/Tool. However, currently, it works with cloud storage(Aspose Internal Storage, Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage, FTP Storage). In near future, we will provide support to merge files from the request body(stream).


const { PdfApi } = require("asposepdfcloud");
const { MergeDocuments }= require("asposepdfcloud/src/models/mergeDocuments");
var fs = require('fs');

pdfApi = new PdfApi("xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxx");

const file1 = "dummy.pdf";
const file2 = "02_pages.pdf";
const localTestDataFolder = "C:\\Temp";

const names = [file1, file2];
const resultName = "MergingResult.pdf";
const mergeDocuments = new MergeDocuments();
mergeDocuments.list = []; 
names.forEach( (file) => {
        mergeDocuments.list.push(file);
    });


// Upload File
pdfApi.uploadFile(file1, fs.readFileSync(localTestDataFolder + "\\" + file1)).then((result) => {  
                     console.log("Uploaded File");    
                    }).catch(function(err) {
    // Deal with an error
    console.log(err);
});

// Upload File
pdfApi.uploadFile(file2, fs.readFileSync(localTestDataFolder + "\\" + file2)).then((result) => {  
                     console.log("Uploaded File");    
                    }).catch(function(err) {
    // Deal with an error
    console.log(err);
});


// Merge PDF documents
pdfApi.putMergeDocuments(resultName, mergeDocuments, null, null).then((result) => {    
    console.log(result.body.code);    
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});

//Download file
const outputPath = "C:/Temp/" + resultName;

pdfApi.downloadFile(outputPath).then((result) => {    
    fs.writeFileSync(localPath, result.body);
    console.log("File Downloaded");    
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});

Upvotes: 0

Zach Esposito
Zach Esposito

Reputation: 727

HummusJS is another PDF manipulation library, but without a dependency on PDFtk. See this answer for an example of combining PDFs in Buffers.

Upvotes: 8

idmean
idmean

Reputation: 14875

What do you expect to get when you do Buffer.concat([pdf1, pdf2])? Just by concatenating two PDFs files you won’t get one containing all pages. PDF is a complex format (basically one for vector graphics). If you just added two JPEG files you wouldn’t expect to get a big image containing both pictures, would you?

You’ll need to use an external library. https://github.com/wubzz/pdf-merge might work for instance.

Upvotes: 20

Related Questions