Blank PDF when served from Express

Similar to #1555 but that one was closed so not sure if my comment will get any attention.
Copying my comment from there.

I am running into similar issue with my first ever nodejs service. I have following:

var headers = {} 
///Setting some custom headers for authentication

var urlObj = new URL( url );
var options = {
            host: urlObj.host,
            path: urlObj.pathname + urlObj.query,
            headers: headers
 };
 http.get( options, function( response ) {
            var body = '';
            response.on( 'data', function( data ) {
                body += data;
             } );

             response.on( 'end', function() {
                  res.set( 'Content-Type', 'application/pdf' );
                  res.send( new Buffer( body, 'binary' ) );
             } );
 } );

But I see a blank PDF with the correct number of pages and browser renders it as a PDF but all pages are empty.
Accessing the url directly shows the right pdf so the pdf is fine.
Any input will be really appreciated.

Thanks

Author: Fantashit

1 thought on “Blank PDF when served from Express

  1. Thanks. Yes request module really simplified the code a lot to basically one liner.

    request.get( { url: url, headers: headers } ).pipe( res );

Comments are closed.