How to Output a Pdf Buffer to Browser Using Nodejs

How to send a pdf file from Node/Express app to the browser

You have to pipe from Readable Stream to Writable stream not the other way around:

var file = fs.createReadStream('./public/modules/datacollectors/output.pdf');
var stat = fs.statSync('./public/modules/datacollectors/output.pdf');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=quote.pdf');
file.pipe(res);

Also you are setting encoding in wrong way, pass an object with encoding if needed.

how to convert a buffer to pdf on front end

On the frontend, you can create ObjectURL that can be downloaded using

const url = window.URL.createObjectURL(new Blob([this.props.resumePDF]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'yourcoolpdf.pdf');
document.body.appendChild(link);
link.click();


Related Topics



Leave a reply



Submit