關于編寫viewer,關鍵在于使用ReportEngine API,這個在
birt官方文檔
上有很詳細的描述。 這里將幾個主要環節總結一下:
-
啟動ReportEngine
這里需要注意啟動ReportEngine的開銷問題和圖片鏈的協議的問題。使用IReportEngineFactory比每次new一個出來性能方面要好很多。使用HTMLEmitterConfig可以使得生成的HTML報表中的圖片的src指向一個web資源而非file資源。
public IReportEngine getEngine() {
???? try {
?????? if (platformContext == null ) {
???????? platformContext = new PlatformServletContext(servletContext);
?????? }
?????? engineConfig = new EngineConfig();
?????? engineConfig.setEngineHome(
?????????? servletContext.getRealPath( " /WEB-INF/platform/ " ));
?????? // 要求ENGINE HOME 位于WEB-INF/Platform
?????? engineConfig.setPlatformContext(platformContext);
?????? // This call sets the Log directory name and level
?????? engineConfig.setLogConfig(getLogDir(), Level.FINE);
??????
?????? // 設置Emitter,渲染HTML的時候,image的地址是HTTP協議而不是File協議
?????? HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
?????? emitterConfig.setActionHandler( new HTMLActionHandler());
?????? HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
?????? emitterConfig.setImageHandler(imageHandler);
?????? engineConfig.getEmitterConfigs().put( " html " , emitterConfig);
??????
?????? // 設置日志level
?????? engineConfig.setLogConfig(getLogDir(), Level.WARNING);
?????? // 啟動Platform,創建ReportEngine
?????? Platform.startup(engineConfig);
?????? IReportEngineFactory factory = (IReportEngineFactory) Platform
?????????? .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
?????? engine = factory.createReportEngine(engineConfig);
?????? engine.changeLogLevel(Level.WARNING);
??????
?????? log.debug( " A new engine startup. " );
???? } catch (BirtException e) {
?????? e.printStackTrace();
???? }
???? return engine;
?? }
-
運行報表
我理解為編譯報表文件。BIRT在渲染報表之前,要將報表編譯為.rptdocument,再根據這個文件將報表渲染為HTML格式或PDF格式。編譯報表使用IRunTask:
protected void run(String rptDesign) {
???? assert (context != null );
???? IReportEngine engine = context.getEngine();
???? // Open a report design
???? IReportRunnable design = null ;
???? try {
?????? design = engine.openReportDesign(context
?????????? .getFullRptDesignFilename(rptDesign));
?????? // Create task to run the report - use the task to
?????? // execute the report and save to disk.
?????? IRunTask task = engine.createRunTask(design);
?????? String doc = context.getFullRptDocumentFilename(rptDesign);
?????? // run the report and destroy the engine
?????? task.run(doc);
??????
?????? log.debug( " save rpt design to " + doc);
?????? task.close();
???? } catch (EngineException e) {
?????? e.printStackTrace();
???? }
?? }
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
