pdf/html [message #152207] |
Mon, 10 April 2006 08:32  |
Eclipse User |
|
|
|
Hi
I create a java program to generate PDF & html from .rptdesign file . I
put all rptdesign in database & it create pdf & html successfully.
This is my code
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public static void run( InputStream name, HashMap params,String format,
boolean showInfo )
{
EngineConfig config = new EngineConfig( );
config.setEngineHome( "C:/jakarta-tomcat-5.0.27/webapps/learnity/" );
HTMLEmitterConfig hc = new HTMLEmitterConfig( );
HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
hc.setImageHandler( imageHandler );
config.setEmitterConfiguration( HTMLRenderOption.OUTPUT_FORMAT_HTML, hc
);
ReportEngine engine = new ReportEngine( config );
IReportRunnable report = null;
try
{
report = engine.openReportDesign( name );
}
catch ( EngineException e )
{
System.err.println( "Report " + name + " not found!\n" );
engine.destroy( );
return;
}
if ( format != null )
{
IRunAndRenderTask task = engine.createRunAndRenderTask( report );
HTMLRenderOption options = new HTMLRenderOption( );
options.setOutputFormat( format );
options.setOutputFormat(format);
OutputStream oooo=null;
try
{
oooo=new
FileOutputStream("C:/jakarta-tomcat-5.0.27/webapps/learnity/ggggg.pdf ") ;
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found="+e);
}
options.setOutputStream( oooo );
task.setRenderOption( options );
task.setParameterValues( params );
try
{
task.run( );
task = null;
}
catch ( EngineException e1 )
{
System.err.println( "Report run failed.\n" );
System.err.println( e1.toString( ) );
}
}
engine.destroy( );
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession mysession=request.getSession(true);
OutputStream out_frame = response.getOutputStream( );
String reportName = request.getParameter("selectreport");
System.out.println("reportName=="+reportName);
String reportType = request.getParameter("selecttype");
System.out.println("reportType=="+reportType);
Vector reportFile=ReportEngineDatabaseLayer.downrpt(reportName);
InputStream in=null;
if (reportFile != null) {
for(int j=0;j<reportFile.size();j++){
Vector vModule = (Vector)reportFile.elementAt(j);
in = (InputStream)vModule.elementAt(1);
}
}
boolean showInfo = false;
HashMap params = new HashMap();
String tfor = new String();
String format ="";
if(reportType.equals("HTML"))
{
format="html";
System.out.println("HTML File");
}
else{
format="pdf";
response.setContentType("application/pdf");
}
run( in, params, format, showInfo);
response.setContentType("application/pdf");
String mmm="C:/jakarta-tomcat-5.0.27/webapps/learnity/ggggg.pdf";
returnFile(mmm,out_frame);
}
public static void returnFile(String filename, OutputStream fout)
throws FileNotFoundException, IOException
{
InputStream in = null;
try
{
in = new BufferedInputStream(new FileInputStream(filename));
byte[ ] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = in.read(buf)) != -1)
{
fout.write(buf, 0, bytesRead);
}
}
finally
{
if (in != null) in.close( );
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
But when i pass outputstream in place of "oooo=new
FileOutputStream("C:/jakarta-tomcat-5.0.27/webapps/learnity/ggggg.pdf ") ;"
then
can not generate pdf /html.
this is my 2nd source which does not generate pdf/html.
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
static OutputStream out1=null;
public static void run( InputStream name, HashMap params,String format,
boolean showInfo )
{
EngineConfig config = new EngineConfig( );
config.setEngineHome( "D:/jakarta-tomcat-5.0.27/webapps/learnity/" );
HTMLEmitterConfig hc = new HTMLEmitterConfig( );
HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
hc.setImageHandler( imageHandler );
config.setEmitterConfiguration( HTMLRenderOption.OUTPUT_FORMAT_HTML, hc
);
ReportEngine engine = new ReportEngine( config );
IReportRunnable report = null;
try
{
report = engine.openReportDesign( name );
}
catch ( EngineException e )
{
System.err.println( "Report " + name + " not found!\n" );
engine.destroy( );
return;
}
if ( format != null )
{
IRunAndRenderTask task = engine.createRunAndRenderTask( report );
HTMLRenderOption options = new HTMLRenderOption( );
options.setOutputFormat( format );
options.setOutputFormat(format);
options.setOutputStream( out1 );
task.setRenderOption( options );
task.setParameterValues( params );
try
{
task.run( );
task = null;
}
catch ( EngineException e1 )
{
System.err.println( "Report run failed.\n" );
System.err.println( e1.toString( ) );
}
}
engine.destroy( );
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession mysession=request.getSession(true);
OutputStream out_frame = response.getOutputStream( );
String reportName = request.getParameter("selectreport");
System.out.println("reportName=="+reportName);
String reportType = request.getParameter("selecttype");
System.out.println("reportType=="+reportType);
Vector reportFile=ReportEngineDatabaseLayer.downrpt(reportName);
InputStream in=null;
if (reportFile != null) {
for(int j=0;j<reportFile.size();j++){
Vector vModule = (Vector)reportFile.elementAt(j);
in = (InputStream)vModule.elementAt(1);
}
}
boolean showInfo = false;
HashMap params = new HashMap();
String tfor = new String();
String format ="";
if(reportType.equals("HTML"))
{
format="html";
System.out.println("HTML File");
}
else{
format="pdf";
System.out.println("pdf");
response.setContentType("application/pdf");
}
run( in, params, format, showInfo);
if(reportType.equals("HTML"))
{
format="html";
System.out.println("HTML File");
}
else{
format="pdf";
response.setContentType("application/pdf");
}
returnFile(out1,out_frame);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
|
|
|
Re: pdf/html [message #152356 is a reply to message #152207] |
Mon, 10 April 2006 14:15  |
Eclipse User |
|
|
|
Originally posted by: jarif.despammed.com
jay wrote:
> Hi
> I create a java program to generate PDF & html from .rptdesign file . I
> put all rptdesign in database & it create pdf & html successfully.
Congratulations!
> This is my code
>
However, you use InputStream variable named "name", which makes the code
very unreadable. You even say
>catch ( EngineException e )
>{
> System.err.println( "Report " + name + " not found!\n" );
> engine.destroy( );
> return;
>}
How in earth does that compile? As the "name" is not a String but
InputStream??
|
|
|
Powered by
FUDForum. Page generated in 0.03750 seconds