function PDFMerge(pdfFiles, pdftkPath) { if(!_(pdfFiles).isArray() || pdfFiles.length === 0) { throw new Error('pdfFiles must be an array of absolute file paths.'); } this.pdftkPath = pdftkPath; if(pdftkPath && !fs.existsSync(pdftkPath)) { throw new Error('Path to PDFtk is incorrect.'); } //Windows: Demand path to lib if(isWindowsPlatform()) { this.isWin = true; } //Array of files this.pdfFiles = pdfFiles; //Get an available temporary filePath to be used in PDFtk this.tmpFilePath = tmp.tmpNameSync(); //Setup Arguments to be used when calling PDFtk this.execArgs = assembleExecArgs.call(this); //Default Mode 'BUFFER'; this.mode = 'BUFFER'; //Default dont keep the temporary file this.keepTmpFile = false; return this; }
n/a
function PDFMerge(pdfFiles, pdftkPath) { if(!_(pdfFiles).isArray() || pdfFiles.length === 0) { throw new Error('pdfFiles must be an array of absolute file paths.'); } this.pdftkPath = pdftkPath; if(pdftkPath && !fs.existsSync(pdftkPath)) { throw new Error('Path to PDFtk is incorrect.'); } //Windows: Demand path to lib if(isWindowsPlatform()) { this.isWin = true; } //Array of files this.pdfFiles = pdfFiles; //Get an available temporary filePath to be used in PDFtk this.tmpFilePath = tmp.tmpNameSync(); //Setup Arguments to be used when calling PDFtk this.execArgs = assembleExecArgs.call(this); //Default Mode 'BUFFER'; this.mode = 'BUFFER'; //Default dont keep the temporary file this.keepTmpFile = false; return this; }
n/a
function PDFMerge(pdfFiles, pdftkPath) { if(!_(pdfFiles).isArray() || pdfFiles.length === 0) { throw new Error('pdfFiles must be an array of absolute file paths.'); } this.pdftkPath = pdftkPath; if(pdftkPath && !fs.existsSync(pdftkPath)) { throw new Error('Path to PDFtk is incorrect.'); } //Windows: Demand path to lib if(isWindowsPlatform()) { this.isWin = true; } //Array of files this.pdfFiles = pdfFiles; //Get an available temporary filePath to be used in PDFtk this.tmpFilePath = tmp.tmpNameSync(); //Setup Arguments to be used when calling PDFtk this.execArgs = assembleExecArgs.call(this); //Default Mode 'BUFFER'; this.mode = 'BUFFER'; //Default dont keep the temporary file this.keepTmpFile = false; return this; }
n/a
asBuffer = function () { this.mode = 'BUFFER'; return this; }
...
```javascript
var PDFMerge = require('pdf-merge');
var pdftkPath = 'C:\\PDFtk\\bin\\pdftk.exe';
var pdfFiles = [__dirname + '/pdf1.pdf', __dirname + '/pdf2.pdf'];
var pdfMerge = new PDFMerge(pdfFiles, pdftkPath);
pdfMerge
.asBuffer()
.merge(function(error, buffer) {
//fs.writeFileSync(__dirname + '/merged.pdf', buffer);
});
```
...
asNewFile = function (path) { this.mode = 'NEWFILE'; this.newFilePath = path; return this; }
...
var pdfMerge = new PDFMerge([...]);
pdfMerge.asReadStream().merge(function(error, readStream) {...});
```
**asNewFile(`filePath`)** -- Store the output in a new file at the given filePath.
```
var pdfMerge = new PDFMerge([...]);
pdfMerge.asNewFile('merged.pdf').merge(function(error, filePath) {...});
```
**keepTmpFile()** -- Keep the temporary file that is created when running PDFtk. For whatever reason..
```
var pdfMerge = new PDFMerge([...]);
pdfMerge.keepTmpFile().merge(function(error, buffer) {...});
```
...
asReadStream = function () { this.mode = 'READSTREAM'; return this; }
...
var pdfMerge = new PDFMerge([...]);
pdfMerge.merge(function(error, result) {});
```
**promise()** -- Promise style. Result is mixed, see the Chainable Options below. By default, a Buffer is always returned.
```
var pdfMerge = new PDFMerge([...]);
pdfMerge.asReadStream().promise()
.then(function(readStream) {...})
.catch(function(error) {...});
```
### API - Chainable Settings
**asBuffer()** -- Result will be a Buffer of the merged PDF document.
```
...
keepTmpFile = function () { this.keepTmpFile = true; return true; }
...
var pdfMerge = new PDFMerge([...]);
pdfMerge.asNewFile('merged.pdf').merge(function(error, filePath) {...});
```
**keepTmpFile()** -- Keep the temporary file that is created when running PDFtk. For whatever reason..
```
var pdfMerge = new PDFMerge([...]);
pdfMerge.keepTmpFile().merge(function(error, buffer) {...});
```
...
merge = function (callback) { var mode = this.mode; var keepTmpFile = this.keepTmpFile; //Keep the Tmp File? var tmpFilePath = this.tmpFilePath; //Filepath for PDF file being created by PDFtk var newFilePath = this.newFilePath; //MODE === 'NEWFILE' //Windows or not, different syntax if(this.pdftkPath) { child.execFile(this.pdftkPath, this.execArgs, execCallbackHandler) } else { child.exec('pdftk ' + this.execArgs.join(' '), execCallbackHandler); } /** * ErrorHandler for when PDFtk has been executed. * @param error * @returns {*} */ function execCallbackHandler(error) { if(error) { return callback(error); } /** * BUFFER/NEWFILE processed the same way. * For NEWFILE, it stores the buffer in a new file. */ if(mode === 'BUFFER' || mode === 'NEWFILE') { fs.readFile(tmpFilePath, function(error, buffer) { if(error) { return callback(error); } deleteFile(); if(mode !== 'NEWFILE') { return callback(null, buffer); } fs.writeFile(newFilePath, buffer, function(error) { return callback(error, newFilePath); }) }); } else if(mode === 'READSTREAM') { var readStream = fs.createReadStream(tmpFilePath); callback(null, readStream); readStream.on('end', function() { deleteFile(); }); } } /** * Cleanup the temporary file created through PDFtk. * Don't cleanup if keepTmpFile === true */ function deleteFile() { if(!keepTmpFile) { fs.unlink(tmpFilePath, function() {}); } } }
...
var PDFMerge = require('pdf-merge');
var pdftkPath = 'C:\\PDFtk\\bin\\pdftk.exe';
var pdfFiles = [__dirname + '/pdf1.pdf', __dirname + '/pdf2.pdf'];
var pdfMerge = new PDFMerge(pdfFiles, pdftkPath);
pdfMerge
.asBuffer()
.merge(function(error, buffer) {
//fs.writeFileSync(__dirname + '/merged.pdf', buffer);
});
```
### Usage
...
promise = function () { var def = Q.defer(); this.merge(function(error, result) { if(error) { return def.reject(error); } def.resolve(result); }); return def.promise; }
...
//Callback-style
pdfMerge.merge(function(error, result) {
//Handle your error / result here
});
//Promise-style
pdfMerge.promise().then(function(result) {
//Handle result
}).catch(function(error) {
//Handle error
});
```
### API - Main
...