2017-04-22 6 views
1

J'ai besoin d'un peu d'aide Je veux générer un rapport pdf.Téléchargement d'un fichier à partir du serveur HapiJs

J'utilise PDFKit nœud Module

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') 

} 

Sur le côté client, je l'ai essayé tant de choses:

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> 

Comment télécharger le fichier PDF? Cela semble être une tâche simpe, mais je ne suis pas capable de le faire.

Merci.

Répondre

0

.text ne ressemble pas à une chaîne à partir des exemples du PDFKit readme. C'est une fonction à utiliser comme doc.text('Hello world!').

Je l'ai testé avec l'itinéraire suivant:

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

Et je ce code html pour le fichier à télécharger le 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>