Hi Henri,
1. put commandButton on JSF to call action method of managed bean; the
PDF will open in new browser window.
<h:form id="yourform" target="_blank">
<h:commandButton styleClass="btn" value="#{msg.btn_pdf}"
action="#{reportMBean.createPdf}" />
2. and use something like this in your reportMBean
public String createPdf() {
FacesContext ctx = FacesContext.getCurrentInstance();
if(!ctx.getResponseComplete()) {
try {
String baseDir = getBaseDir(ctx);
HttpServletResponse response = (HttpServletResponse)
ctx.getExternalContext().getResponse();
byte[] bytes = buildContent(baseDir);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;
filename=\"your_report_name.pdf\"");
response.setHeader("Cache-Control", "max-age=10");
response.setContentLength(bytes.length);
OutputStream output = response.getOutputStream();
output.write(bytes, 0, bytes.length);
output.flush();
output.close();
ctx.responseComplete();
}
catch(BusinessException ex) {
// our BusinessException handling
addBusinessExceptionMsg(ex);
return NAV_SAME_PAGE;
}
catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
// we have all report-files in "/WEB-INF/report/"
private String getBaseDir(FacesContext ctx) {
String path = ((ServletContext)
ctx.getExternalContext().getContext()).getRealPath("/WEB-INF/");
return path + File.separator + "report" + File.separator;
}
private byte[] buildContent(String baseDir) throws Exception {
Map parameters = new HashMap();
parameters.put("ReportTitle", "your_title");
// etc. put all other needed parameters
parameters.put("REPORT_RESOURCE_BUNDLE", getResourceBundle());
parameters.put("REPORT_LOCALE", getResourceLocale());
List dataList = yourBusinessService.getDataForReport(your_parameters);
return JasperRunManager.runReportToPdf(baseDir +
"your_report_name.jasper",
parameters,
new
JRBeanCollectionDataSource(dataList));
}
Good-luck
Jozef Hribik
Delbrouck, Henri-Philippe wrote:
Hi,
Could anybody tell me if it is possible to use JasperReport with
MyFaces in order to generate pdf and xls file.
Is there any documentation about that.
Thanks for any help.
Henri-Philippe Delbrouck
__________ Informacia od NOD32 1.1189 (20050808) __________
Tato sprava bola preverena antivirusovym systemom NOD32.
http://www.eset.sk
__________ Informacia od NOD32 1.1189 (20050808) __________
Tato sprava bola preverena antivirusovym systemom NOD32.
http://www.eset.sk