How to redirect the output of reporting engine to a output stream [message #148492] |
Mon, 27 March 2006 09:54  |
Eclipse User |
|
|
|
Originally posted by: tomar_vivek.indiatimes.com
Hello ,
I have written a code for generating the PDF using BIRT runtime engine.
At the moment the file is written on the disk.
I have a requirement to throw the file back to the client using JSP back
as a stream.
How can i do it . Please explain using an example.
Thanking you
Best Regards
Vivek
|
|
|
Re: How to redirect the output of reporting engine to a output stream [message #148500 is a reply to message #148492] |
Mon, 27 March 2006 09:59   |
Eclipse User |
|
|
|
Originally posted by: tomar_vivek.indiatimes.com
Mentioned below is the JSP code that i have used.
Please help in modifying this code so that
the output can be thrown using a ServletOutputStream .
===========================================
<%@ page import ="java.util.HashMap" %>
<%@ page import ="org.eclipse.birt.report.engine.api.EngineConfig" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.HTMLRenderContext" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.HTMLRenderOption" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.IReportRunnable" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.IRunAndRenderTask" %>
<%@ page import ="org.eclipse.birt.report.engine.api.ReportEngine" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.EngineConstants" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.EngineException" %>
<%
ServletContext context = getServletContext();
executeReport( context);
%>
<%!
public EngineConfig config = null ;
public IReportRunnable report = null;
public void executeReport(ServletContext context) throws EngineException
{
// Ist Step
//Engine Configuration - set and get temp dir, BIRT home, Servlet context
config = new EngineConfig();
config.setEngineHome(
(String)context.getInitParameter("BIRT_REPORT_ENGINE") );
// 2nd Step
//Create the report engine
ReportEngine engine = new ReportEngine( config );
// 3rd Step
//Open a report design - use design to modify design, retrieve embedded
images etc.
String name =
"c:/jakarta-tomcat-4.1.27/webapps/tms/paramReport.rptdesign";
try
{
report = engine.openReportDesign( name );
}
catch ( EngineException e )
{
System.err.println( "Report " + name + " not found!\n" );
engine.destroy( );
return;
}
// 5th Step
//Create task to run the report - use the task to execute and run the
report,
IRunAndRenderTask task = engine.createRunAndRenderTask(report);
task.setParameterValue("message","Hello World");
// 6th Step
//Set Render context to handle url and image locataions
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setImageDirectory("image");
HashMap contextMap = new HashMap();
contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
renderContext );
task.setAppContext( contextMap );
//Set rendering options - such as file or stream output,
//output format, whether it is embeddable, etc
HTMLRenderOption options = new HTMLRenderOption();
String format = "pdf";
options.setOutputFormat(format);
String output = name.replaceFirst( ".rptdesign", "." + format );
options.setOutputFileName( output );
task.setRenderOption( options );
//run the report and destroy the engine
task.run();
engine.destroy();
}
%>
|
|
|
Re: How to redirect the output of reporting engine to a output stream [message #148507 is a reply to message #148500] |
Mon, 27 March 2006 10:18  |
Eclipse User |
|
|
|
Originally posted by: tomar_vivek.indiatimes.com
Hello All ,
I managed to find the answer to the above query myself.
The JSP source code is as below for reference.
=======================================================
<%@ page import ="java.util.HashMap" %>
<%@ page import ="java.io.*" %>
<%@ page import ="org.eclipse.birt.report.engine.api.EngineConfig" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.HTMLRenderContext" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.HTMLRenderOption" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.IReportRunnable" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.IRunAndRenderTask" %>
<%@ page import ="org.eclipse.birt.report.engine.api.ReportEngine" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.EngineConstants" %>
<%@ page
import ="org.eclipse.birt.report.engine.api.EngineException" %>
<%
ServletContext context = getServletContext();
ByteArrayOutputStream pdfStream;
pdfStream = executeReport( context);
response.setContentLength(pdfStream.size());
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;
filename=Pdf_Report.pdf");
ServletOutputStream sos = response.getOutputStream();
try {
pdfStream.writeTo(sos);
pdfStream.close();
sos.flush();
}
catch(Exception e) {
e.printStackTrace();
}
pdfStream.reset();
pdfStream = null;
System.gc();
%>
<%!
public EngineConfig config = null ;
public IReportRunnable report = null;
public ByteArrayOutputStream executeReport(ServletContext context) throws
EngineException
{
// Ist Step
//Engine Configuration - set and get temp dir, BIRT home, Servlet context
config = new EngineConfig();
config.setEngineHome(
(String)context.getInitParameter("BIRT_REPORT_ENGINE") );
// 2nd Step
//Create the report engine
ReportEngine engine = new ReportEngine( config );
// 3rd Step
//Open a report design - use design to modify design, retrieve embedded
images etc.
String name =
"c:/jakarta-tomcat-4.1.27/webapps/tms/paramReport.rptdesign";
try
{
report = engine.openReportDesign( name );
}
catch ( EngineException e )
{
System.err.println( "Report " + name + " not found!\n" );
engine.destroy( );
return null ;
}
// 5th Step
//Create task to run the report - use the task to execute and run the
report,
IRunAndRenderTask task = engine.createRunAndRenderTask(report);
task.setParameterValue("message","Hello World");
// 6th Step
//Set Render context to handle url and image locataions
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setImageDirectory("image");
HashMap contextMap = new HashMap();
contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
renderContext );
task.setAppContext( contextMap );
//Set rendering options - such as file or stream output,
//output format, whether it is embeddable, etc
HTMLRenderOption options = new HTMLRenderOption();
String format = "pdf";
options.setOutputFormat(format);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
options.setOutputStream(baos);
task.setRenderOption( options );
//run the report and destroy the engine
task.run();
engine.destroy();
return baos;
}
%>
|
|
|
Powered by
FUDForum. Page generated in 0.03646 seconds