从HapiJs服务器下载文件

从HapiJs服务器下载文件

问题描述:

我需要一点帮助 我想生成pdf报告。从HapiJs服务器下载文件

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

谢谢。

.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'); 
    } 
} 

而且我用这个网站到文件下载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>