BIRT报告为PDF格式,带有横向布局

时间:2013-06-20 14:14:57

标签: java birt

我遇到了BIRT报告生成器的问题。我使用设计器创建了一个报告,并将其主页面方向设置为横向,页面类型设置为A4,使用我的服务器的报表引擎无法使其工作(它始终呈现纵向方向)。如果我省略pdfOptions添加,问题仍会出现。

但是,当我使用设计师的预览选项时,它可以正常工作。

那是我的ReportRenderer课程:

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IPDFRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.engine.api.ReportEngine;
import org.springframework.web.context.ServletContextAware;

public class ReportRenderer{

    public ByteArrayOutputStream renderReport(ReportPath reportPath,
            Map<String, Object> reportParams,
            Locale locale) throws EngineException {

        IReportEngine engine;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        EngineConfig config = new EngineConfig();
        engine = new ReportEngine(config);

        final IReportRunnable design = engine
                .openReportDesign(this._ServletContext.getRealPath("/") 
                   + reportPath.get_Path());

        // design.get
        // engine.
        // Create task to run and render the report,
        final IRunAndRenderTask task = engine.createRunAndRenderTask(design);

        //report arguments and language
        task.setParameterValue("data_url", this._DataUrl);
        task.setParameterValue("user_name", this._UserName);
        task.setParameterValue("user_password", this._UserPassword);
        for (Entry<String, Object> entry : reportParams.entrySet()) {
            task.setParameterValue(entry.getKey(), entry.getValue());
        }
        task.setLocale(locale);

        // Set parent classloader for engine
        task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
                GenericReportRenderer.class.getClassLoader());

        final IRenderOption options = new RenderOption();
        options.setOutputFormat("pdf");

        options.setOutputStream(os);

        final PDFRenderOption pdfOptions = new PDFRenderOption(options);
        pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW,
                IPDFRenderOption.FIT_TO_PAGE_SIZE);
        pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW,
                IPDFRenderOption.OUTPUT_TO_MULTIPLE_PAGES);


        task.setRenderOption(options);

        // run and render report
        task.run();
        task.close();
        return os;
    }
}

似乎可以选择使用javascript更改页面方向,this link说,但我不知道在哪里应用它。我正在使用birt运行时4.2.0。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我终于设法让它在没有为引擎设置类加载器的情况下工作,并且没有特定的pdf渲染选项:

ReportEngine engine = new ReportEngine(new EngineConfig());

// open design document
IReportRunnable runnable = engine.openReportDesign(this._ServletContext
    .getRealPath("/")
    + reportPath.get_Path());

IRunAndRenderTask task = engine.createRunAndRenderTask(runnable);

for (Entry<String, Object> ent : reportParams.entrySet()) {
    task.setParameterValue(ent.getKey(), ent.getValue());
}

task.setParameterValue("data_url", this._DataUrl);
task.setParameterValue("user_name", this._UserName);
task.setParameterValue("user_password", this._UserPassword);

task.setLocale(locale);

final IRenderOption options = new RenderOption();
options.setOutputFormat("pdf");
options.setOutputStream(os);

task.setRenderOption(options);
task.run();

task.close();

答案 1 :(得分:-1)

您应该将其添加到您的代码中:

options.setMasterPageContent(true);
options.setOutputMasterPageMargins(true);

答案 2 :(得分:-1)

Xtreme Biker'解决方案对我不起作用。

我的问题是我在rptdesign文件中没有方向设置。

经过两天的浏览,我发现以下工作:

IReportRunnable runnableReport = engine.openReportDesign("myreport.rptdesign");

ReportDesignHandle designHandle = (ReportDesignHandle) runnableReport.getDesignHandle();
designHandle.getMasterPages().get(0).setStringProperty("orientation", "landscape");

(与问题中链接的javascript代码相同)

我还必须符合我的内容:

PDFRenderOption pdfOptions = new PDFRenderOption(options);
pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE);

作为替代解决方案(我后来发现它 - 感谢Xtreme Biker)GUI中有一个选项来强制方向Designer GUI option to force orientation

但是,我必须以编程方式处理页面溢出