'How to send a pdf file from Node/Express app to the browser

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[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");
       }    
});

Solution 4:[4]

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

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Christopher Chalfant
Solution 3 Renuka Misal
Solution 4 kdhayal
Solution 5 Ahmed Abdalrahman