Export Jasper Report As PDF And Save Into FileSystem
public void savePdfInvoice(String inputFileName, String outputFileName, Collection dataCollection, Map inputParameters) throws Exception {
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String jrxmlReportPath = context.getRealPath("/WEB-INF/resources/report/" + inputFileName + ".jrxml");
try {
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlReportPath);
JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(dataCollection);
/*if (jrbcds == null) {
System.out.println("....JRBCDS is null");
}
if (inputParameters == null) {
System.out.println("....inputParameters is null");
} */
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, inputParameters, jrbcds);
String outputFile = context.getRealPath("/WEB-INF/resources/invoice/" + outputFileName + ".pdf");
JRExporter exporter = new JRPdfExporter();
/*
* Saves the PDF file generated inside
* ....../build/web/WEB-INF/resources/invoice/filename.pdf
*/
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFile);
exporter.exportReport();
} catch (Exception e) {
System.out.println("JASPER EXCEPTION :: ");
e.printStackTrace();
}
}
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String jrxmlReportPath = context.getRealPath("/WEB-INF/resources/report/" + inputFileName + ".jrxml");
try {
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlReportPath);
JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(dataCollection);
/*if (jrbcds == null) {
System.out.println("....JRBCDS is null");
}
if (inputParameters == null) {
System.out.println("....inputParameters is null");
} */
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, inputParameters, jrbcds);
String outputFile = context.getRealPath("/WEB-INF/resources/invoice/" + outputFileName + ".pdf");
JRExporter exporter = new JRPdfExporter();
/*
* Saves the PDF file generated inside
* ....../build/web/WEB-INF/resources/invoice/filename.pdf
*/
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFile);
exporter.exportReport();
} catch (Exception e) {
System.out.println("JASPER EXCEPTION :: ");
e.printStackTrace();
}
}
Please do a better formatting for readability
ReplyDelete