title: How to download PDF file in angular generated by Springboot at runtime?
body: <p>I am trying to download a runtime generated PDF file (PDF generated using iTextPDF) in Springboot Service and want to open that file in new tab via angular 6.0.
  I tried to follow this <a href="https://stackoverflow.com/questions/57497402/how-to-download-a-pdf-file-in-angular-which-is-generated-on-my-serverspringboot">How to download a pdf file in angular which is generated on my server(springboot)?</a> but getting error as 'Failed to load PDF document'.
  What is wrong in code?</p>
  
  <p>Angular Compomnent:</p>
  
  <pre><code>this.service.PrintPDF(this.quotations).subscribe((data: Blob) =&gt; {
        var file = new Blob([data], { type: 'application/pdf' })
        var fileURL = URL.createObjectURL(file);
  
  // if you want to open PDF in new tab
        window.open(fileURL); 
        var a         = document.createElement('a');
        a.href        = fileURL; 
        a.target      = '_blank';
        // a.download    = 'bill.pdf';
        document.body.appendChild(a);
        a.click();
      },
      (error) =&gt; {
        console.log('getPDF error: ',error);
      }
    );
  </code></pre>
  
  <p>Angular Service:</p>
  
  <pre><code>PrintPDF(quotations: Quotation[]) {
  
          let url = this.PDF_URL;
          var authorization = 'Bearer ' + sessionStorage.getItem("key");
  
          const headers = new HttpHeaders({
              'Content-Type': 'application/json',
              responseType: 'blob',
              "Authorization": authorization
          });
  
          return this.http.post&lt;Blob&gt;(url, quotations, {
              headers: headers, responseType:'blob' as 'json'}).pipe(map(
              (response) =&gt; {
                  return response;
              },
              (error) =&gt; {console.log(error.json());}
          ));
  </code></pre>
  
  <p>Springboot Controller:</p>
  
  <pre><code>@PostMapping(value = "/generatepdf")
      public void downloadFile(@RequestBody List&lt;QuotationDTO&gt; quotationDTOs, HttpServletRequest request, HttpServletResponse response) {
  
          try {
              quotationService.GeneratePDF(quotationDTOs, request, response);
          } catch (DocumentException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p>Springboot Service:</p>
  
  <pre><code>public String  GeneratePDF(java.util.List&lt;QuotationDTO&gt; quotationDTOs, HttpServletRequest request,
                             HttpServletResponse response) throws DocumentException, IOException {
  
          response.setContentType("application/blob");
  
          // Response header
          response.setHeader("Pragma", "public");
          response.setHeader("responseType", "blob");
          response.setHeader("Content-Disposition", "attachment; filename=\"" + "Quotation" + "\"");
  //        OutputStream os = response.getOutputStream();
  
          Document document = new Document(PageSize.A4, 50, 50, 5 , 5);
          String current = sdf.format(new Timestamp(System.currentTimeMillis())).toString();
          String reportName = "report"+current;
  
          PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(&lt;Path&gt;));
          document.open();
          // Write Content
          document.close();
          PdfWriter.getInstance(document, response.getOutputStream());
  
          return reportName+".pdf";
      }
  </code></pre>