2

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?

Aritz
  • 30,971
  • 16
  • 136
  • 217

3 Answers3

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();
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • Which parameters were you passing through _reportParams_ ? – OGrandeDiEnne Jun 13 '15 at 13:35
  • The ones that your report is going to use to render the data, @OGrandeDiEnne. It doesn't really matter for this general case. – Aritz Jun 13 '15 at 13:37
  • It does for the specific question: the code you posted does not have any option to tell BIRT to generate landscape pdfs.. or what am I missing ? – OGrandeDiEnne Jun 13 '15 at 13:39
  • It's not something you pass as a parameter at all, but something you configure in the report template for an specific page. My problem was that even if setting it as landscape in the template, it was rendered as portrait for the pdf format only. – Aritz Jun 13 '15 at 13:43
  • And how did you solve the problem ? I have the same issue. – OGrandeDiEnne Jun 13 '15 at 13:50
  • I ommited the `PDFRenderOption pdfOptions` part, as I stated in the answer. Just using `RenderOption` to tell the task you want a PDF output. – Aritz Jun 13 '15 at 13:53
-1

You should add this to your code:

options.setMasterPageContent(true);
options.setOutputMasterPageMargins(true);
Dominique
  • 4,272
  • 1
  • 16
  • 21
  • Isn't it only available for the [HTML renderer](http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.birt.doc.isv%2Fengine%2Fapi%2Forg%2Feclipse%2Fbirt%2Freport%2Fengine%2Fapi%2FIHTMLRenderOption.html)? – Aritz Jun 24 '13 at 10:33
  • In Birt runtime 4.4.2 there are no _setMasterPageContent_ _setOutputMaster_ on the _IRenderOption_ – OGrandeDiEnne Jun 14 '15 at 08:44
-1

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 Designer GUI option to force orientation

However, I had to handle the page overflow programmatically.

OGrandeDiEnne
  • 872
  • 7
  • 14
  • Are you using the same version of the engine? If yes, can you see the `orientation` attribute in your report template's xml code? It could be a bug in your report editor not attaching it. Quite strange. Anyway, thanks for sharing it. – Aritz Jun 13 '15 at 16:38
  • I am using the latest 4.4.2 – OGrandeDiEnne Jun 13 '15 at 16:40
  • I didn't dig much into the template XML. The only orientation I see is _ltr_ – OGrandeDiEnne Jun 13 '15 at 16:48
  • Then that's your problem. The editor isn't adding the orientation property properly, so the engine can't use it. You should have kind of that in your template: ` landscape ...` Have a look at [this](https://github.com/gluck/birt/blob/master/engine/org.eclipse.birt.report.engine.tests/test/org/eclipse/birt/report/engine/layout/pdf/168804.xml) template example. – Aritz Jun 13 '15 at 17:01