title: How to return PDF file from controller
body: <p>I try to return a PDF file from a Controller Endpoint using NestJs. When not setting the <code>Content-type</code> header, the data returned by <code>getDocumentFile</code> gets returned to the user just fine. When I add the header however, the return I get seems to be some strange form of a GUID, the response always looks like this: <code>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</code> where <code>x</code> is a lowercase hexadecimal character. It also seems to be totally unrelated to the actual return value of the handler function, as I even get this strange GUID-thing when not returning anything at all.</p>
  
  <p>When not setting <code>Content-type: application/pdf</code>, the function returns the data of the buffer just fine, however I need to set the header in order to get the browser to recognize the response as a PDF file which is important for my use case.</p>
  
  <p>The controller looks like this:</p>
  
  <pre><code>@Controller('documents')
  export class DocumentsController {
    constructor(private documentsService: DocumentsService) {}
  
    @Get(':id/file')
    @Header('Content-type', 'application/pdf')
    async getDocumentFile(@Param('id') id: string): Promise&lt;Buffer&gt; {
      const document = await this.documentsService.byId(id)
      const pdf = await this.documentsService.getFile(document)
  
      // using ReadableStreamBuffer as suggested by contributor
      const stream = new ReadableStreamBuffer({
        frequency: 10,
        chunkSize: 2048,
      })
      stream.put(pdf)
      return stream
    }
  }
  </code></pre>
  
  <p>and my DocumentsService like this:</p>
  
  <pre><code>@Injectable()
  export class DocumentsService {
    async getAll(): Promise&lt;Array&lt;DocumentDocument&gt;&gt; {
      return DocumentModel.find({})
    }
  
    async byId(id: string): Promise&lt;DocumentDocument&gt; {
      return DocumentModel.findOne({ _id: id })
    }
  
    async getFile(document: DocumentDocument): Promise&lt;Buffer&gt; {
      const filename = document.filename
      const filepath = path.join(__dirname, '..', '..', '..', '..', '..', 'pdf-generator', 'dist', filename)
  
      const pdf = await new Promise&lt;Buffer&gt;((resolve, reject) =&gt; {
        fs.readFile(filepath, {}, (err, data) =&gt; {
          if (err) reject(err)
          else resolve(data)
        })
      })
      return pdf
    }
  }
  </code></pre>
  
  <p>I originally just returned the buffer (<code>return pdf</code>), but that brought the same result as the attempt above. On the repository of NestJs a user suggested to use the above method, which obviously does not work for me either. See the GitHub thread <a href="https://github.com/nestjs/nest/issues/1090" rel="noreferrer">here</a>.</p>