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: 
Usually if you are using phantom to generate a pdf then the file will be written to disc and you have to supply the path and a callback to the render function.

```
router.get('/pdf', function(req, res){
    // phantom initialization and generation logic
    // supposing you have the generation code above 
    page.render(filePath, function (err) {
        var filename = 'myFile.pdf';
        res.setHeader('Content-type', "application/pdf");
        fs.readFile(filePath, function (err, data) {
            // if the file was readed to buffer without errors you can delete it to save space
            if (err) throw err;
            fs.unlink(filePath);

            // send the file contents
            res.send(data);
        });
    });
});

```
I don't have experience of the frameworks that you have mentioned but I would recommend using a tool like Fiddler to see what is going on. For example you may not need to add a content-length header since you are streaming and your framework does chunked transfer encoding etc.