node.js - 从 HapiJs 服务器下载文件

标签 node.js pdf hapi.js pdfkit

我需要一点帮助 我想生成一份 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/

相关文章:

node.js - Chrome 在重定向后不发送 cookie

php - 如何在php中使用不同的表字段分别插入pdf和video

node.js - 如何在 Joi/Hapi 中设置最大图像尺寸

node.js - 使用 NVM( Node 版本管理器)bash : line 5: npm: command not found 安装时,Laravel Envoy 在 Ubuntu 上无法检测 Node/npm

javascript - Cheerio 通过选择器解析 html 时遇到的问题

node.js - 内部 500 错误 - Node.js - express-session

javascript - node.js 白板应用程序的客户端或服务器端 HTML5 Canvas 渲染?

c++ - 如何从源代码构建 libpoppler?

javascript - Html 打印为 PDF - 如果表格在第二页上拆分,则不应用 Css

javascript - Seneca-web 超时配置