BIRT report as PDF with landscape layout

烈酒焚心 提交于 2019-12-11 02:04:46

问题


I'm facing an issue with BIRT report generator. I created a report using the designer and having set its masterpage orientation to landscape and page type to A4, there's no way to get it working using the report engine of my server (it always renders with the portrait orientation). If I ommit pdfOptions adding, the problem still appears.

However, it works when I use designer's preview option.

That's my ReportRenderer class:

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;
    }
}

It seems to be an option to change the page orientation using javascript as this link says, but I don't know where to apply that. I'm using birt runtime 4.2.0.

Any idea?


回答1:


I finally managed to get it working without setting the class loader for the engine and without specific options for pdf rendering:

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();



回答2:


You should add this to your code:

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



回答3:


Xtreme Biker' solution did not work for me.

My problem was that I did not have the orientation setting in the rptdesign file.

After two days of browsing, I've found the following working:

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

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

(that is the same of the javascript code linked in the question)

I also had to fit my content:

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

As alternative solution (I found it later - thanks Xtreme Biker) there is an option in the GUI to force the orientation

However, I had to handle the page overflow programmatically.



来源:https://stackoverflow.com/questions/17215978/birt-report-as-pdf-with-landscape-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!