Pdfkit: How to merge two pdfs

Created on 26 Aug 2015  路  5Comments  路  Source: foliojs/pdfkit

I am trying to merge two pdf files in a single pdf using node and pdfkit. But, I am only able to get pdf with two blank pages. Looks like addContent is completely ignored or I am using it incorrectly (most likely). Here is the code:

var express = require('express');
var app = express();
var PDFDocument = require('pdfkit');
var fs = require('fs');

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.render('pages/index');
});

app.get('/merge', function(request, response) {
    var doc = new PDFDocument();
    var c1 = fs.readFileSync('./pdfs/1.pdf');
    var c2 = fs.readFileSync('./pdfs/2.pdf');
    doc.addContent(c1);
    doc.addPage();
    doc.addContent(c2);
    doc.end();
    doc.pipe(response);
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

Most helpful comment

All 5 comments

Merging pdfs is not supported by PDFKit. I suggest using another tool, such as pdftk https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

Thanks

Just want to know if this is supported by now

It's not possible with PDFKit

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edmelly picture edmelly  路  5Comments

cam-intel picture cam-intel  路  6Comments

stephen-last picture stephen-last  路  4Comments

durango picture durango  路  4Comments

Valen558 picture Valen558  路  5Comments