2

I am trying to download a PDF from an API, but am running into a little issue. The data seems to come back fine; here's an example of it coming back:

pdf result

In the Angular app, I'm using the ngx-filesaver package to save the files. It's working with a different PDF, over which I have a little more control than in this case, and with CSVs. So I know that the package works in the right situations. Here's the download file function I'm using:

getGroup1094BTaxForm(year: string = '2017') {
    return this._http.get(`${this.groupBaseUrl}/taxes/1094/${year}`, { responseType: 'text' }).pipe(
        map((res: any) => {
            const blob = new Blob([res], { type: 'application/pdf' });
            this._fileSaver.save(<any>blob, 'group-1094b.pdf');
            return res;
        })
    );
}

I have tried setting the responseType to blob and text on the GET request, and then when I create the blob after the response comes back I explicitly set it to application/pdf. This same issue occurs when I take out the line that creates the blob and just pass the response right into the _fileSaver.save function.

What am I missing for how to save the file without it being blank? I checked out this answer, but none of the solutions worked for me.

pjlamb12
  • 2,300
  • 2
  • 32
  • 64

1 Answers1

0

Change your responseType from text to blob

getGroup1094BTaxForm(year: string = '2017') {
    return this._http.get(`${this.groupBaseUrl}/taxes/1094/${year}`, { responseType: 'blob' }).pipe(
        map((res: any) => {
            const blob = new Blob([res], { type: 'application/pdf' });
            this._fileSaver.save(<any>blob, 'group-1094b.pdf');
            return res;
        })
    );
}
Sunil Singh
  • 11,001
  • 2
  • 27
  • 48