How to view PDF document and download it if we want to, with Blob object?

Viewed 27

Solution:

We have to generate a Blob object and define it as below:

pdfPrint() {
 this.commonservice.PostDownload('serviceName', '', requestBody).subscribe((data: Blob) => {
      const pdf = new Blob([data], { type: 'application/pdf' });
      const url1 = window.URL.createObjectURL(pdf);
      window.open(url1);
      const link = document.createElement('a');
      link.setAttribute('style', 'display: none');
      link.href = url1;
      link.target = '_blank';
      document.body.appendChild(link);
      link.click();
      window.URL.revokeObjectURL(url1);
      link.remove();
    }
}
1 Answers

Solution: As we don't need to revoke the link object. Use the Blob object like this:

isPrint() {
 this.commonservice.PostDownload('serviceName', '', requestBody).subscribe((data: Blob) => {
      const pdf = new Blob([data], { type: 'application/pdf' });
      const url1 = window.URL.createObjectURL(pdf);
      window.open(url1);
      const link = document.createElement('a');
      link.setAttribute('style', 'display: none');
      link.href = url1;
      link.target = '_blank';
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
}
Related