Pdfmake: send the pdf to client without downloading

Created on 21 Jan 2016  路  2Comments  路  Source: bpampuch/pdfmake

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);

Most helpful comment

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:

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kumarandena picture kumarandena  路  3Comments

MathLavallee picture MathLavallee  路  3Comments

michaelqiji picture michaelqiji  路  3Comments

dmatesic picture dmatesic  路  3Comments

Christian24 picture Christian24  路  3Comments