Question: I am working on `meanjs` application generated using <https://github.com/DaftMonk/generator-angular-fullstack>. I am trying to generate a .pdf file using `phantomjs` and download it to the browser.

The issue is that the downloaded .pdf file always shows the blank pages regardless of the number of pages. The original file on server is not corrupt. When I investigated further, found that the downloaded file is always much larger than the original file on the disk. Also this issue happens only with .pdf files. Other file types are working fine.

I've tried several methods like `res.redirect('http://localhost:9000/assets/exports/receipt.pdf');`, `res.download('client\\assets\\exports\\receipt.pdf')`,

```
var fileSystem = require('fs');
var stat = fileSystem.statSync('client\\assets\\exports\\receipt.pdf');
res.writeHead(200, {
                    'Content-Type': 'application/pdf',
                    'Content-Length': stat.size
                });

var readStream = fileSystem.createReadStream('client\\assets\\exports\\receipt.pdf');
return readStream.pipe(res);

```

and even I've tried with <https://github.com/expressjs/serve-static> with no changes in the result.

I am new to `nodejs`. What is the best way to download a .pdf file to the browser?

**Update:**
I am running this on a Windows 8.1 64bit Computer

Answer: 
I had corruption when serving static pdfs too. I tried everything suggested above. Then I found this:
<https://github.com/intesso/connect-livereload/issues/39>
In essence the usually excellent connect-livereload (package ~0.4.0) was corrupting the pdf.
So just get it to ignore pdfs via:

```
app.use(require('connect-livereload')({ignore: ['.pdf']}));

```

now this works:

```
app.use('/pdf', express.static(path.join(config.root, 'content/files')));

```

...great relief.
Here is a clean way to serve a file from express, and uses an `attachment` header to make sure the file is downloaded :

```
var path = require('path');
var mime = require('mime');

app.get('/download', function(req, res){
  //Here do whatever you need to get your file
  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);
});

```