我正在尝试使用 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'));
});