Sunday, May 21, 2017

Export Jasper Report to PDF Stream in Controller



@Autowired
private DataSource dataSource;
@RequestMapping(method = RequestMethod.GET, value = "/secure/ppt/pptPenerimaanPenugasanPP/cetakSurat/{idPermohonan}")
public @ResponseBody void cetakSurat(HttpServletResponse response, HttpServletRequest request, @PathVariable("idPermohonan") Long idPermohonan) throws ClassNotFoundException, SQLException, JRException, IOException {
 Connection con = dataSource.getConnection();
 File reportFile = new File(request.getRealPath("jasper/notis_daftar_lpkt.jasper"));
 if (!reportFile.exists()){
  throw new JRRuntimeException(
    "File notis_daftar_lpkt.jasper not found. The report design must be compiled first.");
 }else{
  System.out.println("file notis_daftar_lpkt.jasper exist");
 }
 JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);

 Map parameters = new HashMap();
 String jasperPath = reportFile.getParent() + "/"; 
 
 
 parameters.put("pIdPermohonan", idPermohonan);
 parameters.put("pImgPath", globalProperties.get("report.image"));
 
 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);

 
 OutputStream out = response.getOutputStream();
 response.setContentType("application/pdf");
 response.setHeader ("Content-Disposition", "inline;filename=notis_daftar_lpkt.pdf");
 JasperExportManager.exportReportToPdfStream(jasperPrint, out);
 
 //Close connection
 con.close();
}

No comments:

Post a Comment