0

I am using JasperExportManager to export pdf as byte[] and then convert it to string to be able to add it to the response JSON object.

In the front end in angular I can't convert it back to a byte array to save it again I may be able to save it with lifesaver lib but it won't open the pdf is corrupted

here is my service code:

InputStream jrxmlFile =
      myclass.class.getResourceAsStream("/myjrxml.jrxml");
//       Map params = new HashMap();
  HashMap reportParameters = new HashMap();
  reportParameters.put("Parameter1", goodsIssueCode);

  JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile);

  JasperPrint report =
      JasperFillManager.fillReport(jasperReport, reportParameters, dataSource.getConnection());
  report.addStyle(prepareNormalStyle());
byte[] pdf = JasperExportManager.exportReportToPdf(report);
// Export pdf file
return getSuccessResponse(pdf.toString());

And my front end function:

mergeMap(response => {
let b: any = new Blob([response['code']], { type: 'application/pdf' });
FileSaver.saveAs(b, 'mypdf.pdf');
return [];
  })
Alex K
  • 22,315
  • 19
  • 108
  • 236
Hossam Hassan
  • 11
  • 1
  • 4

1 Answers1

0

You need to add { responseType: 'blob' }

Try like this:

this.http.get(url, { responseType: 'blob' }).subscribe((resp: any) => {
   ...
});
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79