Bhushan
Bhushan

Reputation: 1547

Birt : How to generate PDF from rtpdesign file in standalone Java application

I have designed very simple student.rptdesign file from Eclipse Luna SR2 IDE for Java and Report Developers. It's Data Source is BIRT POJO Data Source. It is working fine from there. Now what I want to do now is generate same PDF file from Java class(without BIRT Eclipse). I searched lot of articles but they are mostly about 'JDBC datasource'.

Can anyone guide me how to do that ?

Upvotes: 1

Views: 4138

Answers (2)

Snake MAGIC
Snake MAGIC

Reputation: 54

when you create your report in report perspective me i create the report.rptdesign in my src folder and then you install birt from the official website when it done unzip it and access to Plugins add it to classpath in you java app and then it will work you can encouter maybe one error that says no module tidy found then install tidy.jar.after that enjoy it that is a code example try it.

Add this imports

   import org.eclipse.birt.core.exception.BirtException;
   import org.eclipse.birt.core.framework.Platform;
   import org.eclipse.birt.report.engine.api.EngineConfig;
   import org.eclipse.birt.report.engine.api.EngineException;
   import org.eclipse.birt.report.engine.api.HTMLRenderOption;
   import org.eclipse.birt.report.engine.api.IReportEngine;
   import org.eclipse.birt.report.engine.api.IReportEngineFactory;
   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 java.io.File;

private void printer() throws BirtException {
    Thread printThread = new Thread(() -> {
    IReportEngine engine=null;
    EngineConfig config = null;

    try{
        config = new EngineConfig( );           
        System.out.println("1");
        config.setBIRTHome("C:\\Documents and Settings\\__\\ documents\\Dev\\Programmes\\Runtime BIRT\\4_2_2\\ReportEngine");
        config.setLogConfig(".\\logs", java.util.logging.Level.FINEST);
        System.out.println("2");
        Platform.startup( config );
        System.out.println("3");
        IReportEngineFactory factory = (IReportEngineFactory) Platform
        .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
        engine = factory.createReportEngine( config );      
        System.out.println("4");
        IReportRunnable design = null;
        //Open the report design
        design = engine.openReportDesign("path/to/src/report.rptdesign"); //change it
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);         
        task.setParameterValue("ordParam", (new Integer(10101)));
        task.validateParameters();
        System.out.println("5");
                
        HTMLRenderOption options = new HTMLRenderOption();  
        System.out.println("6");
        options.setOutputFileName("output/report.html");
        options.setOutputFormat("html");
        options.setHtmlRtLFlag(false);
        options.setEmbeddable(false);
        //options.setImageDirectory("C:\\test\\images");

        PDFRenderOption options1 = new PDFRenderOption();
        options1.setOutputFileName("test.pdf");
        options1.setOutputFormat("pdf");
        
        System.out.println("7");

        task.setRenderOption(options1);
        System.out.println("8");
        task.run();
        System.out.println("9");
        task.close();
        System.out.println("10");
        engine.destroy();
        System.out.println("11");
        
        Desktop.getDesktop().open(new File("test.pdf"));
        System.out.println("11");
    }catch( Exception ex){
        ex.printStackTrace();
    }       
    finally
    {
           Platform.shutdown( );
    }
    

    
});

// Start the printing thread
printThread.start();

}

This is what i try and its worked You can see i put the function asynchronous because its so slow so for not crashing your app when its load i suggest to do that or maybe try to change pdf render options if you want

Upvotes: 0

Simulant
Simulant

Reputation: 20112

I think this is what you want:

Here is an example from the BIRT documentation on how to call the ReportEngine from a Java main method to generate a report from an existing .rptdesign report.

There is also an example for pdf output in it.

Upvotes: 3

Related Questions