Export Jasper Report As PDF And Content Disposition
public void createPdfInvoice(String inputFileName, String outputFileName, Collection dataCollection, Map inputParameters) throws Exception {
FacesContext fc = FacesContext.getCurrentInstance();
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream = response.getOutputStream();
String jrxmlReportPath = context.getRealPath("/WEB-INF/resources/report/" + inputFileName + ".jrxml");
response.setContentType("application/pdf");
try {
JasperCompileManager.compileReportToFile(jrxmlReportPath);
} catch (Exception e) {
System.out.println("jrxml exception :::: createReportAsPdf");
e.printStackTrace();
}
try {
InputStream reportStream = fc.getExternalContext().getResourceAsStream("/WEB-INF/resources/report/" + inputFileName + ".jasper");
JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(dataCollection);
if (reportStream == null) {
System.out.println("....InputStream is null");
}
if (servletOutputStream == null) {
System.out.println("....servletOutputStream is null");
}
if (jrbcds == null) {
System.out.println("....JRBCDS is null");
}
if (inputParameters == null) {
System.out.println("....inputParameters is null");
}
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, inputParameters, jrbcds);
response.setHeader("", "attachment; filename=" + outputFileName + ".pdf");
servletOutputStream.flush();
servletOutputStream.close();
fc.responseComplete();
} catch (Exception e) {
System.out.println("JASPER EXCEPTION :: ");
e.printStackTrace();
}
}
FacesContext fc = FacesContext.getCurrentInstance();
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream = response.getOutputStream();
String jrxmlReportPath = context.getRealPath("/WEB-INF/resources/report/" + inputFileName + ".jrxml");
response.setContentType("application/pdf");
try {
JasperCompileManager.compileReportToFile(jrxmlReportPath);
} catch (Exception e) {
System.out.println("jrxml exception :::: createReportAsPdf");
e.printStackTrace();
}
try {
InputStream reportStream = fc.getExternalContext().getResourceAsStream("/WEB-INF/resources/report/" + inputFileName + ".jasper");
JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(dataCollection);
if (reportStream == null) {
System.out.println("....InputStream is null");
}
if (servletOutputStream == null) {
System.out.println("....servletOutputStream is null");
}
if (jrbcds == null) {
System.out.println("....JRBCDS is null");
}
if (inputParameters == null) {
System.out.println("....inputParameters is null");
}
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, inputParameters, jrbcds);
response.setHeader("", "attachment; filename=" + outputFileName + ".pdf");
servletOutputStream.flush();
servletOutputStream.close();
fc.responseComplete();
} catch (Exception e) {
System.out.println("JASPER EXCEPTION :: ");
e.printStackTrace();
}
}
Comments
Post a Comment