2009/01/22

Eclipse BIRTで作った帳票イメージをjavaアプリで使うには

以下のURLの記事を参考にした。runtimeが古かったせいで、ここのソースそのままでは動かなかった。
http://www.thinkit.co.jp/cert/tech/28/4/3.htm

まず、Javaアプリを作る前に、BIRTのruntimeが必要。これがないと、BIRTで作った拡張子rpttemplateを生かすことができない。
以下のURLの[Runtime]というボタンクリックでダウンロード。
http://download.eclipse.org/birt/downloads/

ダウンロードして解凍したら、環境変数を設定する(設定方法は基本的なことなので記述しない)。
BIRT_HOME=C:\birt-runtime-2_3_1
こんな感じ。

javaのソースでimportするのは以下のもの
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.engine.api.ReportEngine;


PDFRenderがPDF用, HTMLRenderがHTML用。見てのとおり。

classpathに追加したjarは、以下の通り
C:/birt-runtime-2_3_1/ReportEngine/lib/coreapi.jar
C:/birt-runtime-2_3_1/ReportEngine/lib/engineapi.jar
C:/birt-runtime-2_3_1/ReportEngine/lib/modelapi.jar
C:/birt-runtime-2_3_1/ReportEngine/lib/scriptapi.jar
C:/eclipse/plugins/com.ibm.icu_3.8.1.v20080530.jar
C:/eclipse/plugins/org.apache.commons.codec_1.3.0.v20080530-1600.jar
C:/eclipse/plugins/org.mozilla.rhino_1.6.7.v20080214/lib/js.jar

下から2番目の"codec"とあるものは、base64用。テンプレートに画像を読み込んだので、それをdecodeするためのものと思われる。base64のdecodeがなければ不要かもしれない。

中身は、以下の通り。

public class ReportExample {

  public static void main(String[] args) {
    try {
      EngineConfig config = new EngineConfig();
      config.setEngineHome("C:\\birt-runtime-2_3_1\\ReportEngine");

       // レポートエンジン生成とレポートファイル読み込み
      ReportEngine engine = new ReportEngine(config);
      IReportRunnable design =
        engine.openReportDesign("C:\\workspace\\hello\\new_template.rpttemplate");

       // タスク生成
      IRunAndRenderTask task = engine.createRunAndRenderTask(design);

       if (RenderOption.OUTPUT_FORMAT_HTML.equals(args[0])) {
        // HTML設定
        RenderOption options = new HTMLRenderOption();
        options.setOutputFileName("c:\\storeList.html");
        options.setOutputFormat(RenderOption.OUTPUT_FORMAT_HTML);
         task.setRenderOption(options);
      } else {
        // PDF設定
        RenderOption options = new PDFRenderOption();
        options.setOutputFileName("c:\\storeList.pdf");
        options.setOutputFormat(RenderOption.OUTPUT_FORMAT_PDF);
        task.setRenderOption(options);
      }

       // 実行
      task.run();

       engine.destroy();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

あと、dbからの読込みを行う場合は、jdbcドライバを以下に格納する必要がある。
C:\birt-runtime-2_3_1\ReportEngine\plugins\org.eclipse.birt.report.data.oda.jdbc_2.3.1.v20080827\drivers

0 件のコメント:

コメントを投稿