在 运行 时间将 Birt 报告导出为 PDF 文件
Export a Birt report at run time to a PDF file
如何在 运行 时使用 javascript 导出 birt 报告?
我试过的代码在 After Render 事件中:
importPackage(Packages.org.eclipse.birt.report.engine.api);
rptdoc = reportContext.getReportRunnable().getReportName();
// Open the rptDocument
reportDocument = getReportEngine().openReportDocument(rptdoc);
// Create the render task, configured for the wanted export format
IRenderTask renderTask = getReportEngine().createRenderTask(reportDocument);
IRenderOption options = new RenderOption();
options.setOutputFormat("pdf");
options.setOutputFileName("C:/test/myfile.pdf");
renderTask.setRenderOption(options);
// Launch the render task and close it
renderTask.render();
renderTask.close();
// Close the rptDocument
reportDocument.close();
它 运行s 并且没有错误但是没有生成 pdf...
我的要求是 运行 使用 URL 的 birt 报告,该报告应在文件夹中以 pdf 格式自行生成。
报告应该只执行一次。我不想执行 URL 然后在报告中它在工厂之后执行另一个 运行AndRender,报告应该自行导出。希望这是有道理的。
我正在使用 Birt 4.5。
更新代码。
我可以在报告的 afterRender 事件中使用以下代码让它工作。
//Import Lib for birt this is standard, Import our custom birt report java lib
importPackage(Packages.org.eclipse.birt.report.engine.api);
importPackage(Packages.company.reports.data);
//Creates an instance of our custom class
st = new Packages.company.reports.data.MyStandardFunctions();
//Get Global vaiables
fileDir = reportContext.getGlobalVariable("fileDir");
sTcNumber = reportContext.getGlobalVariable("sTcNumber");
sTcRevNum = reportContext.getGlobalVariable("sTcRevNum");
sTcType = reportContext.getGlobalVariable("sTcType");
sPdfPath = reportContext.getGlobalVariable("sPdfPath");
bHasErrors = reportContext.getGlobalVariable("bHasErrors");
testCertError = reportContext.getGlobalVariable("testCertError");
var re = reportContext.getReportRunnable().getReportEngine();
//Get current dreport
var pathOfReport = reportContext.getReportRunnable().getReportName().replace("file:", "");
//Create a directory for temp export reports TempExport is my dir name.
fileDir = st.createOrGetDir(fileDir);
//Only export to pdf if there were no errors.
if(bHasErrors == false)
{
var des = re.openReportDesign(pathOfReport);
var ntask = re.createRunAndRenderTask(des);
//Set parameters ie. ntask.setParameterValue("sOpiid", params["sOpiid"].value);
ntask.setParameterValue("createPDF", false);
ntask.setParameterValue("TCNumber", sTcNumber);
ntask.setParameterValue("TCRevNum", sTcRevNum);
ntask.setParameterValue("TCType", sTcType);
//The ReportPath were you want to store the file on server(linux) test17.pdf is the name of your file. You can change this.
var outputfile = fileDir + "temp.pdf"; //***** test17.pdf you can change to your file name.
//This is the export options.. this will change when exporting to another format.
var options = new PDFRenderOption();
options.setOutputFileName(outputfile);
options.setOutputFormat("pdf");
ntask.setRenderOption(options);
ntask.run();
ntask.close();
问题是,当从系统中调用报告作为 post 调用时,它会生成两次报告。现在我知道它为什么这样做了,因为它再次执行了 运行andrender。我只希望它 运行 一次并导出为 pdf。
Java 包装器服务是我推荐的。然后您可以使用 RunAndRenderTask 而不是单独的 RunTask 和 RenderTask。
或者您可以使用 Web 查看器 Servlet 并检索 PDF 输出作为 HTTP 响应。
无论如何,如果您尝试从报表本身的 Java脚本中调用任何这些任务,那是错误的做法。
查看者请参阅 https://wiki.eclipse.org/Integration_Examples_(BIRT) for an examples of the Java API and https://www.eclipse.org/birt/documentation/integrating/viewer-usage.php(您需要在 URL 中使用 /运行)。
如何在 运行 时使用 javascript 导出 birt 报告?
我试过的代码在 After Render 事件中:
importPackage(Packages.org.eclipse.birt.report.engine.api);
rptdoc = reportContext.getReportRunnable().getReportName();
// Open the rptDocument
reportDocument = getReportEngine().openReportDocument(rptdoc);
// Create the render task, configured for the wanted export format
IRenderTask renderTask = getReportEngine().createRenderTask(reportDocument);
IRenderOption options = new RenderOption();
options.setOutputFormat("pdf");
options.setOutputFileName("C:/test/myfile.pdf");
renderTask.setRenderOption(options);
// Launch the render task and close it
renderTask.render();
renderTask.close();
// Close the rptDocument
reportDocument.close();
它 运行s 并且没有错误但是没有生成 pdf...
我的要求是 运行 使用 URL 的 birt 报告,该报告应在文件夹中以 pdf 格式自行生成。
报告应该只执行一次。我不想执行 URL 然后在报告中它在工厂之后执行另一个 运行AndRender,报告应该自行导出。希望这是有道理的。
我正在使用 Birt 4.5。
更新代码。 我可以在报告的 afterRender 事件中使用以下代码让它工作。
//Import Lib for birt this is standard, Import our custom birt report java lib
importPackage(Packages.org.eclipse.birt.report.engine.api);
importPackage(Packages.company.reports.data);
//Creates an instance of our custom class
st = new Packages.company.reports.data.MyStandardFunctions();
//Get Global vaiables
fileDir = reportContext.getGlobalVariable("fileDir");
sTcNumber = reportContext.getGlobalVariable("sTcNumber");
sTcRevNum = reportContext.getGlobalVariable("sTcRevNum");
sTcType = reportContext.getGlobalVariable("sTcType");
sPdfPath = reportContext.getGlobalVariable("sPdfPath");
bHasErrors = reportContext.getGlobalVariable("bHasErrors");
testCertError = reportContext.getGlobalVariable("testCertError");
var re = reportContext.getReportRunnable().getReportEngine();
//Get current dreport
var pathOfReport = reportContext.getReportRunnable().getReportName().replace("file:", "");
//Create a directory for temp export reports TempExport is my dir name.
fileDir = st.createOrGetDir(fileDir);
//Only export to pdf if there were no errors.
if(bHasErrors == false)
{
var des = re.openReportDesign(pathOfReport);
var ntask = re.createRunAndRenderTask(des);
//Set parameters ie. ntask.setParameterValue("sOpiid", params["sOpiid"].value);
ntask.setParameterValue("createPDF", false);
ntask.setParameterValue("TCNumber", sTcNumber);
ntask.setParameterValue("TCRevNum", sTcRevNum);
ntask.setParameterValue("TCType", sTcType);
//The ReportPath were you want to store the file on server(linux) test17.pdf is the name of your file. You can change this.
var outputfile = fileDir + "temp.pdf"; //***** test17.pdf you can change to your file name.
//This is the export options.. this will change when exporting to another format.
var options = new PDFRenderOption();
options.setOutputFileName(outputfile);
options.setOutputFormat("pdf");
ntask.setRenderOption(options);
ntask.run();
ntask.close();
问题是,当从系统中调用报告作为 post 调用时,它会生成两次报告。现在我知道它为什么这样做了,因为它再次执行了 运行andrender。我只希望它 运行 一次并导出为 pdf。
Java 包装器服务是我推荐的。然后您可以使用 RunAndRenderTask 而不是单独的 RunTask 和 RenderTask。
或者您可以使用 Web 查看器 Servlet 并检索 PDF 输出作为 HTTP 响应。
无论如何,如果您尝试从报表本身的 Java脚本中调用任何这些任务,那是错误的做法。
查看者请参阅 https://wiki.eclipse.org/Integration_Examples_(BIRT) for an examples of the Java API and https://www.eclipse.org/birt/documentation/integrating/viewer-usage.php(您需要在 URL 中使用 /运行)。