Eugene Goldberg
Eugene Goldberg

Reputation: 15524

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?

Upvotes: 62

Views: 161769

Answers (6)

kdhayal
kdhayal

Reputation: 83

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

Upvotes: 0

Ahmed Abdalrahman
Ahmed Abdalrahman

Reputation: 1

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

Upvotes: 0

Christopher Chalfant
Christopher Chalfant

Reputation: 601

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

Upvotes: 15

Wenlong Jiang
Wenlong Jiang

Reputation: 802

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.

Upvotes: 53

Renuka Misal
Renuka Misal

Reputation: 75

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

Upvotes: 3

hassansin
hassansin

Reputation: 17498

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.

Upvotes: 96

Related Questions