1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| /**
*
*/
package servlet;
/**
* @author antoine
*
*/
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.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<String, Integer> parameters = new HashMap<String, Integer>();
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("E:/install_birt/birt-runtime-2_1_3/ReportEngine");
//config.setEngineHome("C:/Birt/birt-runtime-2_2_0/ReportEngine");
// set log config using ( null, Level ) if you do not want a log
// file
// config.setLogConfig("c:/Birt/logs", Level.FINE);
config.setLogConfig(null, Level.OFF);
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("html", emitterConfig); //$NON-NLS-1$
IReportRunnable design = null;
// Open the report design
design = engine
.openReportDesign("C:/ecitiz_v3.1.1/studio/workspace/servlet_birt/rapport_petiteenfance.rptdesign");
/****/
// design =
// engine.openReportDesign("C:/Birt/birt-runtime-2_2_0/ReportEngine/samples/hello_world.rptdesign");
// Create task to run and render the report,
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
// Set Render context to handle url and image locataions
HTMLRenderContext renderContext = new HTMLRenderContext();
// Set the Base URL for all actions
renderContext.setBaseURL("http://localhost:8080/");
// Tell the Engine to prepend all images with this URL - Note this
// requires using the HTMLServerImageHandler
renderContext.setBaseImageURL("http://localhost:8080/myimages");
// Tell the Engine where to write the images to
renderContext.setImageDirectory("C:/ecitiz_v3.1.1/studio/workspace/servlet_birt/myimages");
// Tell the Engine what image formats are supported. Note you must have
// SVG in the string
// 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();
// Add a scriptable object, which will allow the report developer to put
// script in the report that references this Java object. eg in script
// pFilter.myjavamethod()
// ProcessFilter pf = new ProcessFilter();
// task.addScriptableJavaObject("pFilter", pf);
// Set rendering options - such as file or stream output,
// output format, whether it is embeddable, etc
HTMLRenderOption options = new HTMLRenderOption();
// Remove HTML and Body tags
// options.setEmbeddable(true);
// Set ouptut location
options.setOutputFileName("/rapports/output.pdf");
// options.setOutputFileName("/rapports/output.html");
// Set output format
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
// options.setOutputFormat("html");
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();
}
}
} |
Partager