【问题标题】:Downloading a File from HapiJs Server从 HapiJs 服务器下载文件
【发布时间】:2023-03-26 05:09:01
【问题描述】:

我需要一点帮助 我想生成一个pdf报告。

我正在使用 PDFKit 节点模块

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 文件? 这似乎是一项简单的任务,但我无法完成。

谢谢。

【问题讨论】:

标签: node.js pdf hapijs pdfkit


【解决方案1】:

.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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2013-08-30
    • 2017-07-06
    • 2019-03-26
    相关资源
    最近更新 更多