private void generateReport(PortletRequest resourceRequest , PortletResponse response, Map
String fileType = ParamUtil.getString(resourceRequest, "fileType");
if(fileType != null && fileType != ""){
HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(response);
OutputStream outputStream = httpServletResponse.getOutputStream();
List
try { invoicesList = InvoiceLocalServiceUtil.getInvoices(-1, -1);
} catch (SystemException e) { System.out.println("no list found");
} String path = getPortletContext().getRealPath("/reports/test_jasper.jrxml");
InputStream inputStream = new FileInputStream (path);
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(invoicesList);
byte[] contentArray = null;
try { JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
if(fileType.equalsIgnoreCase("PDF")){
System.out.println("PDF calling");
contentArray = JasperExportManager.exportReportToPdf(jasperPrint);
httpServletResponse.setContentType("application/pdf");
httpServletResponse.setHeader("Content-Disposition","attachment;filename=test."+fileType);
outputStream.write(contentArray);
}else{
httpServletResponse.setContentType("text/csv");
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,";");
exporter.setParameter(JRCsvExporterParameter.RECORD_DELIMITER,"\r\n");
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=testcsv.csv");
StringBuffer buffer = new StringBuffer();
exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, buffer);
exporter.exportReport();
outputStream.write(buffer.toString().getBytes());
httpServletResponse.flushBuffer();
} } catch (JRException e) {
e.printStackTrace();
System.out.println("data not found");
} outputStream.close();
}
}
Required jars for the above code is :
For preparing CSV document use the class JRCsvExporter and for PDF document use the class JasperExportManager. In above code for exporting document in to PDF and CSV formats the code has written. And remaining things are self-explanatory in the code. For downloading document we need to create OutputStream object and pass the appropriate MIME formats to the HttpServletResponse object. And from UI side I have created forms with two submit buttons “Generate CSV Report” and “Generate PDF Report”. While submitting the report I have created ResourceURL because the action we are doing here is ajax request because after downloading the document the page should not refresh. So I created liferay resource url and written serveResource() method implementation in controller side.
If we look on code in controller is : ExportReports.java
@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse response) throws IOException,
PortletException {
String[] invoiceInfo = ParamUtil.getParameterValues(resourceRequest, "invoiceInfo");
generateReport(resourceRequest,response,putParameters(invoiceInfo));
}
private Map
Map
for(String selectedData : invoiceInfo){
selectedData = "show"+selectedData;
parameters.put(selectedData, Boolean.TRUE);
}
return parameters;
}