title: Pdf download in Angular 2 or Angular 4
body: <p>We have designed a page in Angular4, which has Download PDF files functionality in it. When the user clicks on download, my application makes a call to a web API and get the PDF file data. I have used filesaver package for this. Below is the code snippet of it.</p>
  
  <p><strong>Angular4 code:</strong></p>
  
  <pre><code>    downloadPdf(url: string, body: any, basePage: BaseErrorPage, showSpinner: boolean = false, goBackToMaster: boolean = true) {
      if (showSpinner) {
          basePage.spinner.runningRefCountIncrement();
      }
  
      let headers = new Headers({ 
          'UserID': this.storage.UserID,
          'ADUserName': this.storage.ADUserName,
          'Content-Type': 'application/json', 
          'Accept': 'application/pdf'
      });        
  
      let options = new RequestOptions({ headers: headers });
      options.responseType = ResponseContentType.Blob;
  
      this.http.get(
          url).subscribe(
            (response) =&gt; {
              var mediaType = 'application/pdf';
              var blob = new Blob([response.blob], {type: mediaType});
              var filename = 'test.pdf';
              console.log(blob);
              console.log(response);
              saveAs(blob, filename);
            });
  }
  </code></pre>
  
  <p><strong>And this is how my API looks like:</strong></p>
  
  <pre><code>    [Route("download/{invoiceID}")]
      [HttpGet]
      public HttpResponseMessage DownloadInvoice(int invoiceID)
      {
          var result = service.GetPDFByID(invoiceID);
          if (result == null)
          {
              HttpResponseMessage r4 = new HttpResponseMessage(HttpStatusCode.NotFound);
              return r4;
          }
  
          HttpResponseMessage r = new HttpResponseMessage(HttpStatusCode.OK);
          r.Content = new StreamContent(new MemoryStream(result.Data));
          r.Content.Headers.ContentType = new MediaTypeHeaderValue("application/blob");
          r.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
          r.Content.Headers.ContentDisposition.FileName = result.FileName;
  
          return r;
      }
  </code></pre>
  
  <p>The code seems to be working fine to some extent. When I click on download, the API call is hit and response is received. Also, a file is downloaded. However, when I try to open it, it says "Failed to load PDF document." I guess I have done some mistake in the content matching and tried various options, but couldn't succeed.</p>
  
  <p>Does anyone has any inputs on this, please!</p>
  
  <p>Thanks in advance.</p>