I am creating a PDF file from HTML using html-pdf. Now I want to download the file on button click on HTML. I know how to ForceDownload a File. Now I want to know how I can force download the PDF file generated.
PDF generation Code:
var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('PDF.html', 'utf8'); var options = { "height": "9in", // allowed units: mm, cm, in, px "width": "8in" ,orientation : "portrait","header": { "height": "5mm",border: { "top": "2in", // default is 0, units: mm, cm, in, px "right": "1in", "bottom": "2in", "left": "1.5in" }, "base": "", },}; pdf.create(html).toStream(function(err, stream){ stream.pipe(fs.createWriteStream('foo.pdf')); }); File Download Code:
var http = require('http'); http.createServer(function (req, res) { var text_ready = "This is a content of a txt file." res.writeHead(200, {'Content-Type': 'application/force-download','Content-disposition':'attachment; filename=file.txt'}); res.end( text_ready ); }).listen(8080, '127.0.0.1'); console.log('Server running at http://127.0.0.1:8080/'); Is there a way I can combine both of these? Or a better way to download the PDF file generated without saving it? I got both of them to work separately. I am pretty new to Node.js.