You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jonadan <jo...@yahoo.com> on 2008/01/16 13:45:33 UTC

PDF problem on IE from JSP

Hi,

I am generating PDF documents from JSP directly to output stream. However
when I try it from IE, it does not work properly. Sometime nothing happens.
Sometimes, show data corruption error. Sometimes connection error, etc.

I suspect it may be something to do with partial content reading from IE and
Tomcat does not support or my JSP does not take care, etc.

Anyone had the same problems before?

Jonadan.

-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14879788.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Pid <p...@pidster.com>.
Jonadan wrote:
> Hi,
> 
> I am generating PDF documents from JSP directly to output stream. However
> when I try it from IE, it does not work properly. Sometime nothing happens.
> Sometimes, show data corruption error. Sometimes connection error, etc.
> 
> I suspect it may be something to do with partial content reading from IE and
> Tomcat does not support or my JSP does not take care, etc.
> 
> Anyone had the same problems before?
> 
> Jonadan.
> 

Yes, it's pretty common, especially if you're using SSL. Search google.

The best solution I found was to avoid sending the official PDF 
mimetype, send a generic one instead, and force it to download*.

Something like:

response.setContentType("application/x-download-file");
response.setHeader("Content-Disposition",
   "attachment; filename=" + filename + "; size=" + content.length );



(Also because the Acrobat browser plugin used to take so long to start 
up, but I'm not sure if that's still true.)



p

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.
It's just plain HTTP!


Pid-2 wrote:
> 
> Jonadan wrote:
>> BTW, is this problem is specific to Tomcat ONLY? Or also occurs in other
>> servers such as JBoss, and so on? 
> Are you delivering your PDF over an HTTPS connection?
> 

-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14957804.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Pid <p...@pidster.com>.
Jonadan wrote:
> BTW, is this problem is specific to Tomcat ONLY? Or also occurs in other
> servers such as JBoss, and so on? 

Are you delivering your PDF over an HTTPS connection?

It's not a Tomcat problem, it's probably an IE problem.
Some versions of IE have issues with responses that contain combinations 
of the "application/pdf" mime-type in the Content-Type header, 
"no-cache" Cache instructions and HTTPS connections.



p

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.
PS;

Dynamic generation of PDF files involves more than I thought. I noticed that
iText may not generate the same byte strings for the same code. This is from
iText manuals. In addition, database contents may change. So there is no way
I can deliver the same byte stream in case IE reissues partial requests.
Given this type of problems, the following header should be a reasonable
compromise that works;

	response.setContentType("application/pdf;charset=UTF-8");
	response.setHeader("Content-Disposition",
"inline;filename=\"Document.pdf\""); 
	// response.setHeader("Content-Disposition",
"attachment;filename=\"Document.pdf\""); 
	response.setContentLength(baos.size());
	response.setHeader("Cache-Control", "cache"); // to make IE work!
	response.setHeader("Pragma", "cache"); // to make IE work!
	response.setDateHeader("Expires", 1);

Note that browser will cache for the first time. But will abandon next time
for fresh copy! At least current my testing worked.

Regards.



-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14967238.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Jonadan wrote:
 > BTW, is this problem is specific to Tomcat ONLY? Or also occurs in other
 > servers such as JBoss, and so on?

FYI, a while ago I wrote a page on the Struts wiki dealing with serving 
PDFs... while it was meant for Struts, the majority of the information 
is in fact completely generic and applicable here (Tomcat vs. any other 
server doesn't matter, most of my issues... I've had the same issues 
under Tomcat and Websphere).

http://wiki.apache.org/struts/ServingPdfDocuments?highlight=%28pdf%29

Frank

-- 
Frank W. Zammetti
Author of "Practical Ajax Projects With Java Technology"
  (2006, Apress, ISBN 1-59059-695-1)
and "JavaScript, DOM Scripting and Ajax Projects"
  (2007, Apress, ISBN 1-59059-816-4)
and "Practical DWR 2 Projects"
  (2008, Apress, ISBN 1-59059-941-1)
Java Web Parts - http://javawebparts.sourceforge.net
  Supplying the wheel, so you don't have to reinvent it!

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.
BTW, is this problem is specific to Tomcat ONLY? Or also occurs in other
servers such as JBoss, and so on? 
-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14947983.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by MrKimi <ro...@senanque.co.nz>.
In my case every doc has a different name so the 'same document' issue
doesn't apply. I can't tell you if that would be a problem or lot.


Jonadan wrote:
> 
> Thanks!
> After changing "no-cache" to "cache", it worked! But I am not sure whether
> this may deliver the same documents when diiferent documents are
> generated.
> 
-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14938025.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.
Thanks!
After changing "no-cache" to "cache", it worked! But I am not sure whether
this may deliver the same documents when diiferent documents are generated.

The another problem was that "inline" didn't work for JSP urls, say,
".../mydoc.jsp". So I have to use a servlet taking JSP as parameters and use
"RequestDispatcher". It works properly now.

Regards.



MrKimi wrote:
> 
> The critical part of my solution (which I got from someone else's post) is
> the setting of the cache stuff.
> 

-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14936162.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by MrKimi <ro...@senanque.co.nz>.
The critical part of my solution (which I got from someone else's post) is
the setting of the cache stuff.
Setting the mime type is something you have to do and my particular case
depends on the filename type, but as long as it gets set you also need to
set the cache right as well.


Jonadan wrote:
> 
> My output MIME is ALWAYS PDF!
> If I save the generated PDF into normal directory and access is OK. It may
> be one of these;
> 
> 1. IE security system may be blocking certain patterns developed by
> previous errors.
> 2. Tomcat is not handling requests well.
> 
> Regards.
> 
> 

-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14935934.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.
My output MIME is ALWAYS PDF!
If I save the generated PDF into normal directory and access is OK. It may
be one of these;

1. IE security system may be blocking certain patterns developed by previous
errors.
2. Tomcat is not handling requests well.

Regards.

-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14935186.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: PDF problem on IE from JSP

Posted by MrKimi <ro...@senanque.co.nz>.
This solved a similar problem for me....

String mimeType = sc.getMimeType(filename);
FileHelper helper = new FileHelper();
			
InputStream in = helper.fetch(filename, m_dir);
response.setContentType(mimeType!=null?mimeType:"text/html");
response.setHeader("Pragma", "cache");
response.setHeader("Cache-Control", "cache");

OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0)
{
	out.write(buf, 0, count);
}
in.close();
if (mimeType == null)
out.write("</pre></html>".getBytes());
out.close();

-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14905302.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.
Or IE does not like content or does not expect as valid connection.


-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14899461.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: PDF problem on IE from JSP

Posted by Jonadan <jo...@yahoo.com>.

Pitre, Russell wrote:
> 
> response.setContentLength(baos.size());
> 
Hi,

I added the size information as follows;

	response.setContentType("application/pdf;charset=UTF-8");
	// response.setHeader("Content-Disposition",
"inline;filename=\"Document.pdf\""); 
	response.setHeader("Content-Disposition",
"attachment;filename=\"Document.pdf\""); 
	response.setContentLength(baos.size());
	response.setHeader("Cache-Control","no-cache"); 
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", 0);
	
	ServletOutputStream outstream = response.getOutputStream(); 
	baos.writeTo(outstream); 
	// outstream.flush(); 

If I change the disposition to "inline", nothing changes in the browser.
With the "attachement", I get this error message dialog;

==========================================================
Internet Explore cannot download pdf example.jsp from localhost.

Internet Explore was not able to open this Internet site. The requested site
is 
unavailable or cannot be found. Please try again later.
==========================================================

I suspect that Tomcat is not handling partial content request properly. IE
tends 
to read PDF page by page as we read pages.

Regards.







-- 
View this message in context: http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14896465.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: PDF problem on IE from JSP

Posted by "Pitre, Russell" <RP...@shawmut.com>.
How are you writing to the outputstream?  You should be doing something
like this below( taken from
org.springframework.web.servlet.view.document .AbstractPdfView)

private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 4096;

// IE workaround: write into byte array first.
ByteArrayOutputStream baos = new
BteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);

.
.
Your code to write pdf to baos
.
.

// Write content type and also length (determined via byte array).
response.setContentType("application/pdf");
response.setContentLength(baos.size());

// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();



-----Original Message-----
From: Jonadan [mailto:jonadanowens@yahoo.com] 
Sent: Wednesday, January 16, 2008 7:46 AM
To: users@tomcat.apache.org
Subject: PDF problem on IE from JSP


Hi,

I am generating PDF documents from JSP directly to output stream.
However
when I try it from IE, it does not work properly. Sometime nothing
happens.
Sometimes, show data corruption error. Sometimes connection error, etc.

I suspect it may be something to do with partial content reading from IE
and
Tomcat does not support or my JSP does not take care, etc.

Anyone had the same problems before?

Jonadan.

-- 
View this message in context:
http://www.nabble.com/PDF-problem-on-IE-from-JSP-tp14879788p14879788.htm
l
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org