NodeJS & pdfkit - 如何合并两个 pdf
NodeJS & pdfkit - How to merge two pdfs
我正在尝试使用节点和 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'));
});
PDFKit 似乎不支持合并 pdf。有人建议我使用其他工具,例如 pdftk https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
我正在尝试使用节点和 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'));
});
PDFKit 似乎不支持合并 pdf。有人建议我使用其他工具,例如 pdftk https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/