Express server sends an empty PDF file

I have a route that sends a pdf file:

app.get('/teste',function(req,res,next){ res.setHeader('content-type','application/pdf'); res.download(app.get('appPath')+'/teste.pdf'); } 

I tried using other solutions that do more or less the same thing:

 app.get('/teste',function(req,res,next){ res.setHeader('content-type','application/pdf'); fs.createReadStream(app.get('appPath')+'/teste.pdf').pipe(res); } 

and

 app.get('/teste',function(req,res,next){ res.setHeader('content-type','application/pdf'); res.sendfile(app.get('appPath')+'/teste.pdf'); } 

My problem is when I ask for this route in the browser and I get an empty PDF file with the same number of pages as the original file.

I configured my express server using app.use(express.bodyParser()); .

Can anybody help me?

+5
pdf express
source share
2 answers

I saw this when using the connect-livereload middleware. The problem is that connect-livereload injects a js code snippet into the pdf data stream. It can also cause problems with other non-html data. The good news is that this should only cause problems during development (you should not download this middleware during production.)

This has been recently fixed, but many templates include an older version, so check the package.json file and get the latest version if necessary. The latest version with download connection is 0.5.3.

+11
source share

If, for example, the ignore: ['.pdf'] not working, this may be because you are not requesting a resource with the appropriate suffix.

eg. If you request a resource with an identifier:

 localhost:9000/api/export/getExport?destinationId=56179bb36bc51bb00836c3ed 

You cannot filter the file suffix. Instead, you can filter by the path:

 app.use( require( 'connect-livereload' )( { ignore: [ /api\/export\/getExport.*/ ] } ) ); 

Take a look at the other options for connect-livereload .

0
source share

All Articles