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;
      }));
    }

Leave a Reply

Your email address will not be published. Required fields are marked *