Download PDF file from http request in Angular which is generated By API

Angular Component

downlaodPDF(){
      this.apiService.downlaodPDF(this.invoiceId)
      .subscribe(
        (data: Blob) => {
          var file = new Blob([data], { type: 'application/pdf' })
          var fileURL = URL.createObjectURL(file);

         // if you downlaod
          var a         = document.createElement('a');
          a.href        = fileURL; 
          a.download    = 'pdfFileName.pdf';
          a.click();
        },
        (error) => {
          console.log('getPDF error: ',error);
        }
      );
    }

Angular Service:

  downlaodPDF() {
    // your get request URL
    let url =`${environment.apiUrl}/api/invoice/download-app/667`

    return this.http.get(url ,{responseType :'blob' as 'json'})
      .pipe(map(data => {
        return data;
      }));
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ngoni

It really helped me a lot. Thank you so much