我需要一点帮助 我想生成一份 pdf 报告。
我正在使用 PDFKit Node 模块
const PDFDocument = require("pdfkit");
function generatePDF(request, reply) {
let doc = new PDFDocument();
let path = __dirname + "/payments/" + "filename" + ".pdf";
doc.text = "Hello World!";
doc.text = "Hello Me!";
doc.end();
return reply(doc)
.header('Content-disposition', 'attachment; filename=' + 'payments.pdf')
}
在客户端,我尝试了很多东西:
1.
button.addEventListener("click", function (event) {
axios.get('/payment-pdf')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}, false)
2.
<a href="/payment-pdf" download>Export</a>
如何下载 PDF 文件? 这似乎是一个简单的任务,但我无法完成它。
谢谢。
最佳答案
.text
看起来不像 PDFKit readme 中的示例中的字符串。 。它是一个像 doc.text('Hello world!') 一样使用的函数。
我使用以下路线进行了测试:
{
method: 'GET',
path: '/payment-pdf',
config: {
auth: false
},
handler: (request: hapi.Request, reply: hapi.IReply) => {
let doc = new PDFDocument();
doc.text('Hello world!');
doc.text('Hello me!');
doc.end();
reply(doc)
.header('Content-Disposition', 'attachment; filename=payments.pdf');
}
}
我使用这个 html 文件来下载 pdf:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://localhost:4200/payment-pdf" target="_blank">Export</a>
</body>
</html>
关于node.js - 从 HapiJs 服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563691/