aspose file tools*
The moose likes JSP and the fly likes JSP to Display PDF ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP to Display PDF ?" Watch "JSP to Display PDF ?" New topic
Author

JSP to Display PDF ?

Muks Sam
Ranch Hand

Joined: Apr 09, 2004
Posts: 47

Hi Friends, I have following code and PDf is not getting displayed, Can somebody help me on priority pls.

============================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.Date"%>
<%@ page import="org.apache.struts.util.MessageResources"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.dcx.catl.epc.util.*"%>
<%@ page import="javax.servlet.http.HttpSession"%>

<META name="GENERATOR" content="IBM WebSphere Studio">

<TITLE>ShowPdf</TITLE>
</HEAD>
<BODY>
<table class="groupborder" cellspacing="0" cellpadding="0" width="840" border="0">
<tr>
<td>
<table cellspacing="1" cellpadding="0" width="900" border="1">
<tr>
<td class="tableheading">
<%
System.out.println("Starting...." + request.getParameter("pdf").trim());
if(request.getParameter("pdf")!="")
{
String strGraphicsFileName = "";
String strFileName="";

InputStream in = null;

strGraphicsFileName = request.getParameter("pdf").trim();

System.out.println("strGraphicsFileName : " + strGraphicsFileName);

String strURL = "";
if (EpcConstants.getWHERE_AM_I().equalsIgnoreCase("T"))
{
strURL = "ftp://x:x@clcss020.x.com/Output/tsb/html/en_us/dto/pbd2/08/00/22/";
}
if (EpcConstants.getWHERE_AM_I().equalsIgnoreCase("P"))
strURL = "ftp://x:x@clcss020.x.com/Output/tsb/html/en_us/dto/pbd2/08/00/22/";

System.out.println("strGraphicsFullFileName ::: strURL \n" + strURL+strGraphicsFileName);
try
{
URL url = new URL(strURL+strGraphicsFileName);

URLConnection urlc = url.openConnection();
in = urlc.getInputStream();

strFileName = "/EPC Web/EPCWeb/Web Content/jsp/images/" + strGraphicsFileName;

System.out.println("strFileName : " + strFileName);

System.out.println("\n\n\n----------------------------------------");

File parent =null;
File outputFileOld = new File(parent, strFileName);

System.out.println("out File exists " + outputFileOld.exists());

if (outputFileOld.exists())
outputFileOld.delete();
System.out.println("After deleting old file");

File outputFile = new File(parent, strFileName);
FileOutputStream outFile = new FileOutputStream(outputFile);

System.out.println("out File" );
response.setContentType( "application/pdf" );
response.setContentLength((int)outputFile.length());

response.setHeader("Content-Disposition","inline; filename=\""+ strGraphicsFileName +"\"");
//response.setHeader("Cache-Control", "no-cache");

byte[] buf = new byte[8192];

//FileInputStream inStream = new FileInputStream(outputFile);

int c=0;
//while ((c = in.read()) != -1)
outFile.write(c);

while ((c = in.read(buf, 0, buf.length)) > 0)
{
//System.out.println("size:"+c);
outFile.write(buf, 0, c);
}

//inStream.close();
outFile.flush();
outFile.close();

System.out.println("\n\n\n----------------------------------------");
System.out.println("File Found:");
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("File Not Found: "+e);
%>
<center><font color="red" family="verdana" size="2"> <p style="background-color:white">File Not Available !!</p></font></center>
<%
}
catch(Exception me)
{
me.printStackTrace();
System.out.println("Exception in pdf locating "+me);
}
}
else
{
%>
<table border=0 width=600 align=center><tr><td class=columnheader>File not Found. Please check back later.</td><tr></table>
<%
}
%>
</td>
</tr>
</table>
</td>
</tr>
</table>
</BODY>
</HTML>
============================================================================

thanks

Muks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Well, if all that Java code were in a servlet then it might have a chance of working. But as it is you're writing out HTML, not PDF. Look at the first two lines of what you're writing out:Even if you strip away everything except the scriptlets, I expect there will still be some whitespace that you let leak into the response. And that will mess up the PDF you send. Use a servlet instead.
Muks Sam
Ranch Hand

Joined: Apr 09, 2004
Posts: 47

Thanks for reply.
I can't use servlet since it is a Struts framework. pls. suggest.
Thanks
Muks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

I don't know Struts. But I would be extremely surprised if it didn't allow you to write servlets.

After one minute of my Google search for "struts servlets" I found this:

http://www.javareference.com/jrexamples/viewexample.jsp?id=103

Maybe it's relevant to your problem.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

You cannot do this in a JSP for exactly the reasons Paul mentioned. JSPs are solely inteneded to render character response data. Anything else, be it PDF, images or any other binary stream, must be emitted with a servlet.

Originally posted by mukul samak:

I can't use servlet since it is a Struts framework.


Hogwash.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
If you are doing it in struts, then do it from your Struts action.
Its the equivalent of a servlet anyway.
Just return null from your action to indicate to struts that you have already sent the response.
And why are you writing it to a FileOutputStream? That would write the file out onto the server. Get the response output stream to send it back to the browser.

( a little bit of struts specific code, but not much)


Cheers,
evnafets
 
jQuery in Action, 2nd edition
 
subject: JSP to Display PDF ?