wubzz / pdf-merge

Merge multiple PDF Files into a single PDF document
127 stars 32 forks source link

Question #1

Closed btray77 closed 9 years ago

btray77 commented 9 years ago

Thanks for making this. I want to merge a folder of pdfs into a single file. Can you do an example of how you would suggest doing this as efficiently? There will be 100+ pdfs in a folder.

Thanks

wubzz commented 9 years ago

The way PDF-Merge is constructed is for you to give it a list of files, as in absolute path to the files. So basically all you need to do is make an array out of all the filenames in your folder, and use it in the pdfMerge constructor.

The following example should work fine:

var fs = require('fs');
var PDFMerge = require('pdf-merge');

//Get files
var pdfFiles = fs.readdirSync('./path/to/folder/with/100/pdf/files')

//Construct a new pdfMerge instance with the given files
var pdfMerge = new PDFMerge(pdfFiles)

pdfMerge.merge(function(error, result) {

});

I've never tested this module on more than 50 PDF files, so if you run into any issues let me know.

btray77 commented 9 years ago

Thanks for the quick suggestion!
I'll let you know if I have any issues.

pwong2 commented 7 years ago

@wubzz can you complete the sample code up to where all the files inside the './path/to/folder/with/100/pdf/files' are already generated in a specific location? It's my first time to use this and it will be a great benefit for those like me. btw, I'm a windows user. thanks

wubzz commented 7 years ago

@pwong2 Not entirely sure what you mean, but I realize a mistake in the example above.

const PDFMerge = require('pdf-merge');
const path     = '/path/to/folder/with/files';
const pdfFiles = fs.readdirSync(path).map((file) => [path, file].join('/'));

PDFMerge(pdfFiles)
    .then((buffer) => {
        console.log(buffer.toString());
    });
pwong2 commented 7 years ago

@wubzz what if I want to generate those 50 files into a single physical file? what is the location of the generated merged pdfs?

wubzz commented 7 years ago

@pwong2 By default there is no location. You get a Buffer.

If you want PDF-Merge to automatically store the merged file for you, this is how:

PDFMerge(pdfFiles, {output: `/path/to/store/merged/file/name.pdf`})
.then(() => {
//Whatever comes next
});