将运行时的Birt报告导出为PDF文件

时间:2018-01-30 08:14:22

标签: javascript pdf birt

如何在运行时使用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();

它运行且没有错误,但没有生成pdf ...

我的要求是使用URL运行birt报告,报告应在PDF文件夹中生成自己。

报告只应执行一次。我不想执行URL,然后在报告中它在工厂之后执行另一个runAndRender,报告应该自行导出。希望这是有道理的。

我正在使用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(); 

问题是,当使用系统调用报告作为后调用时,它会生成两次报告。现在我知道为什么会这样做,因为它再次进行了运行。我只想让它运行一次并导出为pdf。

1 个答案:

答案 0 :(得分:0)

Java包装器服务是我推荐的。您可以使用RunAndRenderTask,而不是单独的RunTask和RenderTask。

或者您可以使用Web Viewer Servlet并将PDF输出检索为HTTP响应。

无论如何,如果你试图从报告本身的Javascript中调用任何这些任务,这是错误的方法。

请参阅https://wiki.eclipse.org/Integration_Examples_(BIRT)以获取Java API的示例,并查看https://www.eclipse.org/birt/documentation/integrating/viewer-usage.php查看器(您将在URL中使用/运行)。

相关问题