Milindu Sanoj Kumarage
Milindu Sanoj Kumarage

Reputation: 2774

Node.js: PDFkit write to response

I'm using PDFKit (A PDF generation library for Node.js, pdfkit.org) and I want to send a PDF as a response to a client.

# Write the PDF file to disk
doc.write('output.pdf');

The code above writes the PDF file to the disk, but I want it to send as the response. How can I do that?

Upvotes: 3

Views: 3468

Answers (1)

hexacyanide
hexacyanide

Reputation: 91609

Assuming res is your server response object, just do this:

doc.output(function(string) {
  res.end(string);
});

This will send a string representation of the PDF rather than writing it to file. The code above is the compiled CoffeeScript that was in the documentation for PDFKit.

doc.output (string) -> 
  console.log string

Upvotes: 5

Related Questions