Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

PDF's sent through tomcat/apache

Thread view: 
gwoodhouse@gmail.com - 04 Apr 2008 14:33 GMT
Hello everyone,

After wrestling with FOP ive now got to this point:

What my site does is allow people to download dynamically generated
PDF's. I do this with the FOP framework, then stream the file contents
out in the response. The problem with this is that we're using an
Apache -> Tomcat through AJP - after searching the web ive found that
in this configuration large PDF's will not stream correctly to the PDF
plugin giving a "Currupt File" error.

My websearches lead me to beleive its because the PDF plugin needs the
size of the file sent across so it knows to keep itself open to
receiving more data - but at large sizes the plugin refuses to get
more data and says "currupted".

Has anyone ever encountered this problem and figured a way around it?

Graeme
blueparty - 04 Apr 2008 16:44 GMT
On Apr 4, 3:33 pm, "gwoodho...@gmail.com" <gwoodho...@gmail.com>
wrote:
> Hello everyone,
>
[quoted text clipped - 13 lines]
>
> Has anyone ever encountered this problem and figured a way around it?

I have avoided the same problem with ZIP files because I've read that
the problem exists, and it is Internet Explorer specific. I create
temporary file and send it to browser along with content-length
field.

I've read that everything works fine in decent browsers.

DG
Mark Space - 04 Apr 2008 17:51 GMT
> What my site does is allow people to download dynamically generated
> PDF's. I do this with the FOP framework, then stream the file contents
> out in the response. The problem with this is that we're using an
> Apache -> Tomcat through AJP - after searching the web ive found that
> in this configuration large PDF's will not stream correctly to the PDF
> plugin giving a "Currupt File" error.

I've been on the receiving end of this problem.  I've downloaded PDF
from sites implemented with JSP/Servlets and got 0 length files and
"corrupt" PDF files.

I'd be willing to help take a look at this issue if you think it would
help to have an extra set of eyes on it.

Can you reproduce this problem with a simple JSP file?  Can you send me
a PDF that fails (I guess you might not, if it's private.)  I'll try to
reproduce it on my end.  Also, what version of Tomcat are you using?
Ulf - 06 Apr 2008 07:06 GMT
For my project I have a servlet that looks like below to send pdf
files.
I can't tell if it works in all cases, because my project is under
development, and so far I have only tried with small files.

   protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
       OutputStream out = response.getOutputStream();
       ByteArrayOutputStream bs = myPdfLib.getPdf();

       response.setHeader("Expires", "0");
       response.setHeader("Cache-Control", "must-revalidate, post-
check=0, pre-check=0");
       response.setHeader("Pragma", "public");
       response.setContentType("application/pdf");
       response.setContentLength(bs.size());
       bs.writeTo(out);
       out.flush();
   }

/Ulf
gwoodhouse@gmail.com - 09 Apr 2008 10:36 GMT
> For my project I have a servlet that looks like below to send pdf
> files.
[quoted text clipped - 17 lines]
>
> /Ulf
Mark / Ulf

I did a quick hack to see if the problem existed with a random pdf
file that wasnt dynamically generated. I also tried it on a stand
alone tomcat server with the same error. This only occurs with larger
PDF files mind you.

Heres my code:
            FileInputStream fis = new FileInputStream(file);
            byte[] buff = new byte[2048];
            int bytesRead;
            int bytes=0;
            //    This while loop is simply to get the amount of bytes being sent,
couldnt be bothered looking up a better way
            while(-1 != (bytesRead = fis.read(buff, 0, buff.length)))
            {
                for(int i=0; i < buff.length; i++) if(buff[i]==1 ||
buff[i]==0)bytes++;
                for(int i=0; i < buff.length; i++) buff[i] = -1;
            }
            resp.setContentLength(bytes);
            resp.setContentType("application/pdf");
            resp.setHeader("Content-disposition", "filename=List.pdf");
            while(-1 != (bytesRead = fis.read(buff, 0, buff.length)))
            {
                resp.getOutputStream().write(buff, 0, bytesRead);
            }

I know its a messy hack, but i only wanted a quick test. Problem still
exists. If you want to test it on your own tomcat have a go. You
should be able to copy paste the code into an action and have it work
fine. I would assume any large pdf would do, i can't send out the
PDF's we have here as it contains copywrite information. Sorry.

As always any help is greatly appreciated!

Graeme
gwoodhouse@gmail.com - 09 Apr 2008 10:49 GMT
Sorry,

That code should have been:

            File file = new File("C:\\Development\\newfile.pdf");
            FileInputStream fis = new FileInputStream(file);
            byte[] buff = new byte[2048];
            int bytes=0;
            while(fis.read(buff, 0, buff.length)!=-1)
            {
                bytes+=buff.length;
            }
            fis = new FileInputStream(file);
            resp.setContentLength(bytes);
            resp.setContentType("application/pdf");
            resp.setHeader("Content-disposition", "filename=List.pdf");
            while(fis.read(buff, 0, buff.length)!=-1)
            {
                resp.getOutputStream().write(buff, 0, buff.length);
            }
Lew - 09 Apr 2008 12:14 GMT
> Sorry,
>
[quoted text clipped - 16 lines]
>                 resp.getOutputStream().write(buff, 0, buff.length);
>             }

Friendly suggestion: two spaces, or at most four, is plenty of indent for
Usenet.  The example here is just excessive with indents.

Signature

Lew

gwoodhouse@gmail.com - 09 Apr 2008 11:10 GMT
Even more sensible code:

            File file = new File("C:\\Development\\newfile.pdf");
            int bytes= (int)file.length();

            FileInputStream fis = new FileInputStream(file);
            byte[] buff = new byte[2048];
            resp.setContentLength(bytes);
            resp.setContentType("application/pdf");
            resp.setHeader("Content-disposition", "filename=List.pdf");
            while(fis.read(buff, 0, buff.length)!=-1)
            {
                resp.getOutputStream().write(buff, 0, buff.length);
            }

which doesnt work.
Ulf - 09 Apr 2008 14:11 GMT
On Apr 9, 12:10 pm, "gwoodho...@gmail.com" <gwoodho...@gmail.com>
wrote:
> Even more sensible code:
>
[quoted text clipped - 12 lines]
>
> which doesnt work.

Did you try OutputStream.flush()?

/Ulf
gwoodhouse@gmail.com - 09 Apr 2008 14:14 GMT
Sorry Lew,

Copy paste straight from Eclipse. Ill undo the indent next time, didnt
think anyone would mind.

Ulf: Yeah sorry, i left the resp.flush(); resp.close() out.

Graeme
Ulf - 09 Apr 2008 20:53 GMT
On 9 Apr, 15:14, "gwoodho...@gmail.com" <gwoodho...@gmail.com> wrote:
> Ulf: Yeah sorry, i left the resp.flush(); resp.close() out.

I've tried your sample code with different pdf:s and can't reproduce
the error.

Can it be bad luck, different configuration (Sun Java System
Application Server 9.1_01, IE7, Adobe Reader 8) or something else?

/Ulf
Gilbert - 09 Apr 2008 21:59 GMT
> Can it be bad luck, different configuration (Sun Java System
> Application Server 9.1_01, IE7, Adobe Reader 8) or something else?

I'm coming late to this one, but the above comment triggered a brain cell.
Does your code flat-out refuse to work for any browser, or does it only
fail when using IE as the browser?

The reason I ask is that I've had this before. The code worked fine for
Firefox, but not for IE. I'm sorry that I'm no longer in the same job and
don't have access to the code, but I do know that I found the answer on
Google and it's something to do with an HTTP header that IE requires.
Lew - 10 Apr 2008 01:21 GMT
> Sorry Lew,
>
> Copy paste straight from Eclipse. Ill undo the indent next time, didnt
> think anyone would mind.

An apology?  To me?  Wouldn't thanks have been more in line with the
suggestion?  It's not like I made the suggestion for my convenience.

--
Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.