關(guān)于編寫(xiě)viewer,關(guān)鍵在于使用ReportEngine API,這個(gè)在
birt官方文檔
上有很詳細(xì)的描述。 這里將幾個(gè)主要環(huán)節(jié)總結(jié)一下:
-
啟動(dòng)ReportEngine
這里需要注意啟動(dòng)ReportEngine的開(kāi)銷(xiāo)問(wèn)題和圖片鏈的協(xié)議的問(wèn)題。使用IReportEngineFactory比每次new一個(gè)出來(lái)性能方面要好很多。使用HTMLEmitterConfig可以使得生成的HTML報(bào)表中的圖片的src指向一個(gè)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);
??????
?????? // 設(shè)置Emitter,渲染HTML的時(shí)候,image的地址是HTTP協(xié)議而不是File協(xié)議
?????? HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
?????? emitterConfig.setActionHandler( new HTMLActionHandler());
?????? HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
?????? emitterConfig.setImageHandler(imageHandler);
?????? engineConfig.getEmitterConfigs().put( " html " , emitterConfig);
??????
?????? // 設(shè)置日志level
?????? engineConfig.setLogConfig(getLogDir(), Level.WARNING);
?????? // 啟動(dòng)Platform,創(chuàng)建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;
?? }
-
運(yùn)行報(bào)表
我理解為編譯報(bào)表文件。BIRT在渲染報(bào)表之前,要將報(bào)表編譯為.rptdocument,再根據(jù)這個(gè)文件將報(bào)表渲染為HTML格式或PDF格式。編譯報(bào)表使用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();
???? }
?? }
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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