PDF's sent through tomcat/apache

G

gwoodhouse

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
 
B

blueparty

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?

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
 
M

Mark Space

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?
 
U

Ulf

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
 
G

gwoodhouse

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
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==1 ||
buff==0)bytes++;
for(int i=0; i < buff.length; i++) buff = -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
 
G

gwoodhouse

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);
}
 
G

gwoodhouse

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.
 
U

Ulf

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.

Did you try OutputStream.flush()?

/Ulf
 
G

gwoodhouse

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
 
U

Ulf

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
 
G

Gilbert

Ulf said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top