BIRT 保存为PDF

public class PDFReportServiceAccess {
	/** */
	/** 初始化的状态 */
	protected static boolean initStatus = false;

	private static IReportEngine engine = null;

	private static EngineConfig config = null;

	private static IReportRunnable design = null;

	//private static PDFRenderOption ro = null;
	private static PDFRenderOption ro = null;    //   doubley0

	/** */
	/** 初始化资源 */
	public void initilize() {
		if (initStatus == true)
			return;

		try {
			config = new EngineConfig();
			config.setEngineHome("E://Program Files1//apache-tomcat-5.5.26//webapps//birt//WEB-INF//platform"); // birt    doubley1
			// runtime
			// web应用中的报表引擎目录
			config.setLogConfig("E://Program Files1//apache-tomcat-5.5.26//webapps//birt//logs", Level.FINE);        //doubley2

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

			ro = new PDFRenderOption();
			ro.setBaseURL("http://localhost:8080/birt"); // birt runtime
			// web应用
			config.getEmitterConfigs().put("pdf", ro); // 生成pdf格式       doubley3

			initStatus = true;

		} catch (Exception ex) {
			ex.printStackTrace();
			initStatus = false;
		}
	}

	/** */
	/** 释放资源 */
	public void release() {
		engine.shutdown();
		Platform.shutdown();

		initStatus = false;
	}

	protected OutputStream run(String filename, HashMap parameters)
			throws EngineException {
		design = engine.openReportDesign(filename);

		// Create task to run and render the report,
		IRunAndRenderTask task = engine.createRunAndRenderTask(design);
		HashMap contextMap = new HashMap();
		contextMap.put(EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT, ro);
		task.setAppContext(contextMap);
		task.setParameterValues(parameters);
		task.validateParameters();

		OutputStream os = new ByteArrayOutputStream();
		ro.setOutputStream(os);
		ro.setOutputFormat("pdf");       //doubley4
		task.setRenderOption(ro);
		task.run();
		task.close();

		return os;
	}

	/** */
	/**
	 * 生成PDF格式报表,以OutputStream格式返回
	 * 
	 * @param filename
	 *            报表设计文件名全路径
	 * @param parameters
	 *            报表参数
	 * @return ByteArrayOutputStream
	 * @throws EngineException
	 */
	public OutputStream call(String filename, HashMap parameters)
			throws EngineException {
		initilize();
		OutputStream os = run(filename, parameters);
		release();

		return os;
	}

	/** */
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashMap parameters = new HashMap();
		// 三个Report Parameters,名称必须在报表设计文件中预先定义好
		//parameters.put("begindate", "2004/01/01");
		//parameters.put("enddate", "2007/12/31");
		//parameters.put("sql", " where cust_id = 1234567");
		int temp=7;
		parameters.put("mystr1", "7");           //doubley5
		//parameters.put("mystr1", temp);
		

		ByteArrayOutputStream bos = null;

		PDFReportServiceAccess ebr = new PDFReportServiceAccess();

		String filename = "E://Program Files1//apache-tomcat-5.5.26//webapps//birt//new_report1.rptdesign";        //doubley6
		
		try {
			bos = (ByteArrayOutputStream) ebr.call(filename, parameters);

			OutputStream fis = new FileOutputStream("c:/test.pdf");
			bos.writeTo(fis);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(birt)