2

我正在尝试使用 node 和pdfkit将两个 pdf 文件合并到一个 pdf 中。但是,我只能获得带有两个空白页的 pdf。看起来 addContent 被完全忽略了,或者我使用不正确(很可能)。这是代码:

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'));
});
4

1 回答 1

1

Looks like merging pdfs is not supported by PDFKit. I was suggested to use another tool, such as pdftk https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

于 2015-08-26T19:09:23.863 回答