2010-07-23 89 views
1

我想在Linux环境中调用BIRT API。BIRT - 从Linux平台创建PDF报告

我的源代码是一个XML文件,我在该源XML上创建了html,xml和pdf报告。

我的html和xml报告可以正常生成,但PDF报告创建的大小为'0'(无内容)。

但是,如果我从Windows系统运行相同的程序,PDF报告创建正确。

BIRT是否具有平台相关API?

我使用下面的代码...

public static void generateReport(String format, String birtPath, 
     String outputFile, String templateLoc, boolean log, File dataSource, boolean isSummaryReport) 
     throws Exception { 

    File outputFileObj = new File(outputFile + format); 

    if (outputFileObj.exists()) { 
     if (!outputFileObj.delete()) { 
      Main 
        .getLogger() 
        .log(
          "Error: A File with the same name '" 
            + outputFileObj.getName() 
            + "' exists in the result folder and it can't be overwritten." 
            + LINE_SEPARATOR 
            + "If it is opened then close it before generating the report."); 
      ReportingUIErrorWriter.process(ReportingUIErrorProcessor 
        .getString("ERR_CODE_024"), 
        new String[] { outputFileObj.getName() }); 
      systemExit(FAILURE); 
     } 
    } 

    /* Partie initialisation */ 
    ReportEngine engine = null; 
    EngineConfig engineConfig = new EngineConfig(); 
    engineConfig.setEngineHome(birtPath); 
    File logDir = new File(strLogDir); 
    if (!logDir.exists()) { 
     logDir.mkdir(); 
    } 
    if (log) { 
     engineConfig.setLogConfig(strLogDir, Level.INFO); 
    } else { 
     engineConfig.setLogConfig(strLogDir, Level.OFF); 
    } 

    try { 
     engine = new ReportEngine(engineConfig); 
     // Dont remove this code.If birt engine path is valid but it does 
     // not contain 
     // Birt Engine then it will validate Report engine object. 
     engine.getRootScope(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Main 
       .getLogger() 
       .log(
         "Error: Reporting engine path does not contain valid Birt Engine."); 
     ReportingUIErrorWriter.process(ReportingUIErrorProcessor 
       .getString("ERR_CODE_025"), null); 
     systemExit(FAILURE); 
    } 

    IReportRunnable design = null; 
    RenderOptionBase option = new RenderOptionBase(); 

    if ((format.toLowerCase() != "pdf") && (format.toLowerCase() != "html")) 
     format = "pdf"; // format par défaut 
    option.setOutputFormat(format.toLowerCase()); // Format du fichier de 
    // sortie 

    /* Fin initialisation */ 
    /* Préparation de l'édition */ 
    try { 
     design = engine.openReportDesign(templateLoc); 
    } catch (EngineException e) { 
     Main.getLogger().log(
       "Warning : Fail to open report Design file : " 
         + e.getMessage()); 
     ReportingUIErrorWriter.process(ReportingUIErrorProcessor 
       .getString("ERR_CODE_026"), null); 
     systemExit(FAILURE); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     if (e instanceof NullPointerException) { 
      Main.getLogger().log(
        "Error: Reporting engine path is not correct."); 

      // Destroy the engine object. 
      if (engine != null) { 
       engine.destroy(); 
       engine = null; 
      } 
      ReportingUIErrorWriter.process(ReportingUIErrorProcessor 
        .getString("ERR_CODE_027"), null); 
      systemExit(FAILURE); 
     } 
    } 

    try { 
     /* 
     * Changement de source inspiré de 
     * http://jsourcery.com/api/sourceforge/openreports/2.1/org/efs/openreports/engine/BirtReportEngine.source.html 
     */ 
     ReportDesignHandle reportDH = (ReportDesignHandle) design 
       .getDesignHandle(); 

     List birtDataSources = reportDH.getAllDataSources(); 
     Iterator iterator = birtDataSources.iterator(); 

     while (iterator.hasNext()) { 
      OdaDataSourceHandle dataSH = (OdaDataSourceHandle) iterator 
        .next(); 

      try { 
       // Mantis 04034, 04037 : Update FILELIST property for the 
       // data source with name "Source". 
       if (dataSH.getProperty("name").equals("Source")) { 
        dataSH.setStringProperty("FILELIST", dataSource 
          .getAbsolutePath()); 
       } 
      } catch (SemanticException e) { 
       Main.getLogger().log(
         "Warning : SemanticException in path change" 
           + e.getMessage()); 
       systemExit(FAILURE); 
      } 
     } 

     //drop Grid rows containing information about Location Host and Location Components. 
     if(!isSummaryReport){ 
      if(isDetailedLocInfoDisabled){   
       reportDH.getElementByID(1112).dropAndClear(); // delete and clear the location hostname attribute   
       reportDH.getElementByID(1116).dropAndClear(); // delete and clear the location components attribute 
      }    
     } 

     design.setDesignHandle(reportDH); 
     /* Fin changement source */ 
     IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
     /* Fin de la préparation */ 

     /* Création PDF */ 
     option.setOutputFileName(outputFile + format); // Nom du fichier de 
     // sortie 
     task.setRenderOption(option); // Chargement du fichier  

     if (!isSummaryReport) { 
      if (isWithout_DescInConfigFile) { 
       task.setParameterValue("withoutDesc", new Boolean(isWithout_DescInConfigFile).toString()); 
      } 
      if (isWithMessInConfigFile) { 
       task.setParameterValue("withMess", new Boolean(isWithMessInConfigFile).toString()); 
      } 
     }else{ 
      task.setParameterValue("totalDuration", XMLReportParsing.totalDurationString.substring(1, XMLReportParsing.totalDurationString.length()-2)); 
     } 

     /* Fin création PDF */ 
     try { 
      // On crée le fichier où l’on va écrire. S’il existe déjà, on 
      // écrit à la suite 
      task.run(); // Déclenchement de l'édition 
      // ferme le fichier qui log le run de BIRT 
     } catch (EngineException e) { 
      e.printStackTrace(); 
      Main.getLogger().log(
        "Error in Report Generation : " + e.getMessage()); 
      ReportingUIErrorWriter.process(ReportingUIErrorProcessor 
        .getString("ERR_CODE_028"), null); 
      systemExit(FAILURE); 
     } 
     task = null; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Main.getLogger().log(
       "Error in Report Generation : " + e.getMessage()); 
     ReportingUIErrorWriter.process(ReportingUIErrorProcessor 
       .getString("ERR_CODE_028"), null); 
     systemExit(FAILURE); 
    } 
    // Destroy the engine object. 
    if (engine != null) { 
     engine.destroy(); 
     engine = null; 
    } 

    // Chemin A = C:/birt-runtime-2_0_1/Report Engine. Dans mon cas, j'ai 
    // copier/coller le répertoire Report Engine dans mon répertoire lib où 
    // je rassemble toutes mes 
} 

我已经贴上唯一的相关方法的代码。 请帮忙。

回答

0

找到所有空的try块样

} catch (EngineException e) {} 

DO东西与捕获的异常。我现在建议你做

} catch (EngineException e) { 
     throw new RuntimeException("PDF error", e); 
    } 

所以你可以看到堆栈跟踪。

+0

我已经完成了它,但仍然没有成功。实际上,程序并没有抛出任何例外。 它只是创建PDF文件,但与0内容。 – Sudhir 2010-07-23 09:27:22

+0

你需要告诉引擎明确关闭文件吗?你忽略了其中一个引擎调用返回的错误代码吗? – 2010-07-23 09:32:17

+0

对不起,我不明白。我需要告诉引擎明确关闭文件吗?如果是的话,怎么样? 无论我用什么方法,例如task.run(),上面都会返回void。 – Sudhir 2010-07-23 09:49:06