0

I am working on an Angular 9 web application, where in one of the pages, the client expects a PDF to be downloaded after submitting a form.

I can generate the PDF file successfully at the backend (server-side) and return it using a stream. The output of the Web API I developed to return can be seen from following link:

https://pastebin.com/xJScx7A4

At my Angular app, I have tried several ways to open the PDF in the browser or downloading it as a file. In every case, I get something similar to as shown by the screenshot below

enter image description here

First way I tried:

loadReport(){

this.spinner.show();

//////// /////////////// /////////////////////

this.api.loadReport(this.filterVM)
.subscribe(res => {
  console.log(res);
  this.spinner.hide();

  ////// //////////////////////////
  const blob = new Blob([res], { type: 'application/pdf' });
  const url= window.URL.createObjectURL(blob);
  window.open(url);
  ////////// /////////////////////

}, err => {
  console.log(err);
  this.spinner.hide();
});
///// ///////////// //////////////////////////

}

Second way I tried:

  loadReport(){

    this.spinner.show();

    //////// /////////////// /////////////////////    
    this.api.loadReport(this.filterVM)
    .subscribe(res => {
      console.log(res);
      this.spinner.hide();

      ////// //////////////////////////
      this.saveByteArray("Sample Report.pdf", res);
      ////////// /////////////////////

    }, err => {
      console.log(err);
      this.spinner.hide();
    });
    ///// ///////////// //////////////////////////

  }

 saveByteArray(reportName, byte) {
  var blob = new Blob([byte], {type: "application/pdf"});
  var link = document.createElement('a');
  link.href = window.URL.createObjectURL(blob);
  var fileName = reportName;
  link.download = fileName;
  link.click();
};

Output of second way is shown in the below screenshot:

enter image description here

When I try to download the file, I have seen that it is significantly less in size than the size of the HTTP response (9 bytes vs ~16 KB). My Web API HTTP response is sending the following headers:

Content-Type: application/pdf
Content-Length: 16187
Content-Disposition: attachment; filename=Report.pdf; filename*=UTF-8''Report.pdf

I have experimented with code from the following blogs/sites, but no success yet.

Download File from Bytes in JavaScript

Typescript blob filename without link

https://blog.liplex.de/download-file-through-typescript/

https://dev.to/nombrekeff/download-file-from-blob-21ho

How do I download a file with Angular2 or greater

Shuaib
  • 779
  • 2
  • 13
  • 47
  • 1
    Not sure if related, I did answer something similar long ago: https://stackoverflow.com/questions/43270793/angular-2-how-to-display-pdf-file/43803616#43803616 – Ambroise Rabier Sep 10 '20 at 12:37

1 Answers1

1

The issue has been finally solved! Thanks a lot to Ambroise Rabier, who answered a similar issue in Angular 2 how to display .pdf file .... his post led me to https://medium.com/techinpieces/blobs-with-http-post-and-angular-5-a-short-story-993084811af4 (thanks as well)

Changes made:

This my service.ts file (this was not shared in the question), https://pastebin.com/vhucnvAX

Only change: I added one line

responseType: 'blob' as 'json'

in the first function configureOptions() of my service class. I call this function from the constructor of my service.

My component.ts file: https://pastebin.com/d1gB4V8x

Shuaib
  • 779
  • 2
  • 13
  • 47