gpt4 book ai didi

express - 使用流输出pdf

转载 作者:行者123 更新时间:2023-12-05 09:20:20 26 4
gpt4 key购买 nike

如何使用 expressjs 输出 pdf:

var fs = require('fs');
var PdfPrinter = require('pdfmake/src/printer');

app.get('/', function (req, res) {
var printer = new PdfPrinter();

var first = 'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines';

var dd = {
content: [
first,
'Another paragraph'
]
};
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(fs.createWriteStream('basics.pdf')).on('finish',function(){
//success

});
pdfDoc.end();
});

最佳答案

您可以将输出通过管道传输到 res(在确保您设置了正确的 Content-Type 之后):

app.get('/', function (req, res) {
var printer = new PdfPrinter();
var first = 'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines';
var dd = {
content: [
first,
'Another paragraph'
]
};

// Make sure the browser knows this is a PDF.
res.set('content-type', 'application/pdf');

// Create the PDF and pipe it to the response object.
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(res);
pdfDoc.end();
});

(虽然我不能说它能为我生成清晰的 PDF,但独立运行时代码也不会任何 pdfmake 示例)

关于express - 使用流输出pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37816542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com