I am creating the PDF file and then sending it to browser, how can i send it directly to browser without saving it on server.
Please note i am using the server side implementation of node.js
app.get('/', function(req, res) {
var pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('pdfs/new8.pdf'));
pdfDoc.end();
var file = 'pdfs/new8.pdf';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
});
app.listen(3000);
Almost one year, here's a start:
Using express with a GET or POST path:
//...
var printer = new pdfMakePrinter(fontDescriptors)
var doc = printer.createPdfKitDocument(definition)
var chunks = []
var result
doc.on('data', function (chunk) {
chunks.push(chunk)
});
doc.on('end', function () {
result = Buffer.concat(chunks)
response.contentType('application/pdf')
response.send(result)
});
doc.end()
//...
NB: If your request is AJAX, make sure the responseType (on the client side) is an arraybuffer.
Explanation here:
Example using Angular, jquery like:
Answered.
Most helpful comment
Almost one year, here's a start:
Using express with a GET or POST path:
NB: If your request is AJAX, make sure the
responseType(on the client side) is anarraybuffer.Explanation here:
Example using Angular, jquery like: