Download pdf-Datei-API

Ich am erstellen einer Funktion, die erstellt und als pdf-download nach dem button-Klick. Ich bin mit express als backend und reagieren/readux/axios wie vor.

Mein backend läuft auf einem anderen port als meine front.

So, ich bin dem Aufruf der API mit axios, dann express erstellen Sie die pdf-Datei und reagieren mit sendFile.

Beim testen meine post-Anfrage mit dem Postboten klappt alles Super.

Als ich mit es von meiner reagiert app nicht laden Sie die Datei

express

router.post('/', function( req, res, next){

var data = req.body.data

options = {};
//init html string
var html = ejs.renderFile(__dirname + '/template/facture.ejs',  {data: data}, options, function(err, str){
    if(err){
        return err;
    }
    return str;
});
//create pdf
conversion({ html: html}, (err, pdf) => {
    var output = fs.createWriteStream(`documents/factures/${data.Référence}.pdf`);
    pdf.stream.pipe(output);

});

var filepath = path.join(pathToDocument, '/factures/',`${data.Référence}.pdf`);

res.download(filepath);

});

Axios nennen

export function generatePdf(data){
return dispatch => {

    axios.post(`${API_ENDPOINT}api/facture-pdf`,
        { data: data },
        { headers: {
            'Content-Type': 'application/json',
            'x-access-token': localStorage.getItem('token')
        }
    })
    .then(function (response) {  
        return response.data;
    }) 
    .then( pdf => {
        window.open(`${API_ENDPOINT}api/${type}-pdf/${data.Référence}`, '_blank')
    })
}
InformationsquelleAutor Nicks | 2017-11-29
Schreibe einen Kommentar