56

In my Node/Express app I have the following code, which suppose to read a PDF document from a file, and send it to the browser:

var file = fs.createReadStream('./public/modules/datacollectors/output.pdf', 'binary');
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');
res.pipe(file, 'binary');
res.end(); 

I do not get any errors, but I do not get the file in my browser either. What am I doing wrong?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167

6 Answers6

92

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.

hassansin
  • 16,918
  • 3
  • 43
  • 49
45

I think I found your answer in another post Display PDF file in a browser using node js.

After testing your code in Chrome, it immediately starts the download of the PDF file. But if you want to display the content of the PDF file you could try below:

var data =fs.readFileSync('./public/modules/datacollectors/output.pdf');
res.contentType("application/pdf");
res.send(data);

This should directly send PDF content into the browser.

Hope this answers your question.

Wenlong Jiang
  • 722
  • 6
  • 5
14

Here's the easiest way:

app.get('/', (req, res) => res.download('./file.pdf'))

If this gives you trouble. Check the Express.js version or any middlewares that might be necessary.

Cheers

3

Best way to download a PDF on REST API call.

var path = require('path');     
var file = path.join(__dirname, 'file.pdf');    
res.download(file, function (err) {
       if (err) {
           console.log("Error");
           console.log(err);
       } else {
           console.log("Success");
       }    
});
0

If you are using the Swagger/OpenAPI then you can look in responses section of below code. /api/pdf_from_html: post: tags: - PDF description: Create PDF from html produces: - application/pdf consumes: - application/json parameters: - name: contents description: HTML content to convert to pdf in: body required: true schema: $ref: "#/definitions/pdf" responses: 200: description: Returns a PDF encoded in base64 content: application/pdf: schema: type: string format: base64

kdhayal
  • 83
  • 6
0

i guess it is simple if you want to show it in the browser change content disposition header to "inline" instead of "attachment; filename=quote.pdf" inline show it directly in the browser instead of download it, it worked fine for me