1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String objet = request.getParameter("objet");
String reportName = "";
if (objet.equals("commune")) {
CommuneForm commune = (CommuneForm) form;
WriteXml.writeCommune(commune);
reportName = "commune.rptdesign";
}
// get report name and launch the engine
// response.setContentType("text/html");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=test.pdf");
// String reportName = request.getParameter("ReportName");
// String reportName = "test.rptdesign";
ServletContext sc = request.getSession().getServletContext();
birtReportEngine = BirtEngine.getBirtEngine(sc);
// setup image directory
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setBaseImageURL(request.getContextPath() + "/images");
renderContext.setImageDirectory(sc.getRealPath("/images"));
logger.log(Level.FINE, "image directory "
+ sc.getRealPath("/WEB-INF/images"));
System.out.println("stdout image directory "
+ sc.getRealPath("/WEB-INF/images"));
HashMap contextMap = new HashMap();
contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
renderContext);
IReportRunnable design;
try {
// Open report design
design =
birtReportEngine.openReportDesign(sc.getRealPath("/WEB-INF/reports")
+ "/" + reportName);
// create task to run and render report
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
task.setAppContext(contextMap);
// set output options
HTMLRenderOption options = new HTMLRenderOption();
// options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
options.setOutputStream(response.getOutputStream());
task.setRenderOption(options);
// run report
task.run();
task.close();
birtReportEngine = null;
renderContext = null;
contextMap = null;
design = null;
task = null;
// WriteXml.deleteFile();
} catch (Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
return null;
} |
Partager