2016-08-08 34 views
0

即時通訊使用html-pdf包爲節點,我需要發送該文件作爲響應。 這是我在pdf.create功能用快遞發送二進制pdf文件

pdf.create(html, options).toStream((err, stream) => { 
    if(err) throw err; 
    stream.pipe(fs.createWriteStream(__dirname + '/gen.pdf')); 


    var file = fs.createReadStream(__dirname + '/gen.pdf'); 
    //file.pipe(res); 

    res.setHeader('Content-Type', 'application/pdf'); 
    res.setHeader('Content-Disposition', 'attachment; filename="gen.pdf"'); 
    res.sendFile(__dirname+'/gen.pdf'); 
}); 

回答

0

實際的代碼爲什麼不直接流的反應?例如:

pdf.create(html, options).toStream((err, stream) => { 
    if (err) throw err; 
    res.setHeader('Content-Type', 'application/pdf'); 
    res.setHeader('Content-Disposition', 'attachment; filename="gen.pdf"'); 
    stream.pipe(res); 
}); 
+0

我使用** postman **(chrome app)進行測試,並且我得到一個名爲_response.pdf.pdf_的txt文件 –

相關問題