2017-04-22 9 views
1

少し助けが必要です 私は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ファイルをダウンロードできますか? それはsimpeのタスクのようですが、私はそれを行うことができません。

ありがとうございます。

答えて

0

.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をダウンロードするファイルにこのHTMLを使用:

<!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> 
関連する問題