渲染报表BIRT支持HTML格式和PDF格式,这两种报表生成的过程相同,只是设置不同的RenderContext和RenderOptions。我们把相同的部分提取为父类的公共方法,不同的部分留给子类实现: IReportDocument iReportDocument = engine.openReportDocument(docFilename); // Create Render Task IRenderTask task = engine.createRenderTask(iReportDocument); task.addScriptableJavaObject( " jsBirtObject " , getScriptableObject(scriptObj)); if (params != null && ! params.isEmpty()) ... { task.setParameterValues(params); } setRenderContext(task); // 设置Render Context,具体由子类实现 setRenderOptions(task, out); // 设置Render Options,具体由子类实现 task.render(); task.close(); } catch (EngineException e) ... { e.printStackTrace(); } finally ... { engine.shutdown(); } 渲染PDF和HTML报表 PDF /** */ /** * @see AbstractBirtReportParser#setRenderContext(IRenderTask) */ @Override protected void setRenderContext(IRenderTask task) ... { PDFRenderContext renderContext = new PDFRenderContext(); renderContext.setEmbededFont(true); HashMap contextMap = new HashMap(); contextMap.put( EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT, renderContext); task.setAppContext( contextMap ); } /** */ /** * @see AbstractBirtReportParser#setRenderOptions(IRenderTask, OutputStream) */ @Override protected void setRenderOptions(IRenderTask task, OutputStream out) ... { RenderOptionBase options = new RenderOptionBase(); options.setOutputStream(out); options.setOutputFormat(RenderOptionBase.OUTPUT_FORMAT_PDF); task.setRenderOption(options); } HTML我们要生成一个HTML的一部分,而非包含<html/>标记的完整的HTML文档 /** */ /** * @see AbstractBirtReportParser#setRenderContext(IRenderTask) */ @Override protected void setRenderContext(IRenderTask task) ... { HTMLRenderContext renderContext = new HTMLRenderContext(); renderContext.setBaseURL(context.getBaseURL()); //You must define HTMLServerImageHandler to use a URL renderContext.setBaseImageURL(context.getBaseImageURL()); //renderContext.setImageDirectory("myimages"); renderContext.setImageDirectory(context.getImageDirectory()); renderContext.setSupportedImageFormats("JPG;PNG"); HashMap contextMap = new HashMap(); contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext); task.setAppContext( contextMap ); } /** */ /** * @see AbstractBirtReportParser#setRenderOptions(oIRenderTask, OutputStream) */ @Override protected void setRenderOptions(IRenderTask task, OutputStream out) ... { HTMLRenderOption options = new HTMLRenderOption(); options.setOutputStream(out); options.setEmbeddable(true); task.setRenderOption(options); }