Estoy llamando a una API externa que devuelve un archivo PDF y quiero devolver este archivo PDF en la respuesta de mi función de controlador.
En mi clase de controlador:
@Get(':id/pdf') async findPdf(@Param('id') id: string, @Res() res: Response) { const response = await this.documentsService.findPdf(id); console.log(response.data); // this prints the following: // %PDF-1.5 // %���� // 2 0 obj // << /Type /XObject /Subtype /Image /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter // /DCTDecode /Width 626 /Height // 76 /Length 14780>> // stream // and go on... return res .status(200) .header('Content-Type', 'application/pdf') .header('Content-Disposition', response.headers['content-disposition']) .send(response.data); }En Mi clase de servicio:
findPdf(id: string): Promise<any> { return firstValueFrom( this.httpService .get(`/docs/${id}/pdf`) .pipe(map((response) => response)) .pipe( catchError((e) => { throw new BadRequestException('Failed to get PDF.'); }), ), ); }Sin embargo, recibo un archivo PDF en blanco en mi respuesta.
La llamada a la API interna funciona bien, la probé desde Postman y el archivo PDF es correcto.
¿Qué estoy haciendo mal?