gpt4 book ai didi

java - BIRT - 从 Linux 平台创建 PDF 报告

转载 作者:行者123 更新时间:2023-12-01 05:54:58 25 4
gpt4 key购买 nike

我正在尝试在linux环境中调用BIRT API。

我的源是一个 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
}

我只粘贴了相关的方法代码。请帮忙。

最佳答案

查找所有空的 try block ,例如

} catch (EngineException e) {}

执行捕获异常的操作。我现在建议你这样做

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

这样你就可以看到堆栈跟踪。

关于java - BIRT - 从 Linux 平台创建 PDF 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3315715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com