2007年10月1日 星期一

直接在 Java Application 由 BIRT Engine 產生 PDF

Modify from http://wiki.eclipse.org/index.php/Simple_Execute

怎麼從BIRT不透過GUI (WEB-BASED SERVER)直接產生 PDF檔~

Require Birt Runtime Engine , itext.jar, iTextAsian.jar


// Code Start

package tw.idv.marky;

import java.util.HashMap;
import java.util.logging.Level;

import org.eclipse.birt.core.framework.Platform;
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.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.PDFRenderContext;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
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;
public class ExecuteReport {

static void executeReport() throws EngineException
{
HashMap parameters = new HashMap();

String name = “Top Count”;
Integer pvalue = new Integer(4);
parameters.put(name, pvalue);

IReportEngine engine=null;
EngineConfig config = null;
try{

//Configure the Engine and start the Platform
config = new EngineConfig( );
config.setEngineHome( “/Users/Marky/Desktop/BIRT/runtime220/ReportEngine”);
//set log config using ( null, Level ) if you do not want a log file
config.setLogConfig(“/Users/Marky/Desktop/BIRT/logs”, Level.FINE);

Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
engine.changeLogLevel( Level.WARNING );

}catch( Exception ex){
ex.printStackTrace();
}

//Configure the emitter to handle actions and images
HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
emitterConfig.setActionHandler( new HTMLActionHandler( ) );
HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
emitterConfig.setImageHandler( imageHandler );
config.getEmitterConfigs( ).put( “pdf”, emitterConfig ); //$NON-NLS-1$

IReportRunnable design = null;

//Open the report design
design = engine.openReportDesign(“/Users/Marky/Desktop/BIRT/workspace/workspace220/Test/WebContent/test3.rptdesign”);

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

//Set Render context to handle url and image locataions
PDFRenderContext renderContext = new PDFRenderContext();
//Set the Base URL for all actions
//renderContext.setBaseURL(”http://localhost/”);

//to render charts in SVG.
renderContext.setSupportedImageFormats(“JPG;PNG;BMP;SVG”);
HashMap contextMap = new HashMap();
contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
task.setAppContext( contextMap );
//Set parameters for the report
task.setParameterValues(parameters);
//Alternatively set each seperately
//task.setParameterValue(”Top Count”, new Integer(12));
task.validateParameters();

PDFRenderOption options = new PDFRenderOption();
options.setOutputFileName(“/Users/Marky/Desktop/BIRT/workspace/workspace220/Test/WebContent/test3.pdf”);

//Set output format
options.setOutputFormat(“pdf”);
task.setRenderOption(options);

//run the report and destroy the engine
//Note - If the program stays resident do not shutdown the Platform or the Engine
task.run();
task.close();
engine.shutdown();
Platform.shutdown();
System.out.println(“Finished”);
}
/**
* @param args
*/
public static void main(String[] args) {
try
{
executeReport( );
}
catch ( Exception e )
{
e.printStackTrace();
}
}

}

//Code End

沒有留言: