File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Displaying PDF files on browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Displaying PDF files on browser" Watch "Displaying PDF files on browser" New topic

Displaying PDF files on browser

Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
I want to show PDF file in the browser, while i am able to show the pdf file while i am hard-coding the name of the pdf file(like C:\abc.pdf), the same code doesn't work when i am specifying the file name at runtime, Intrestingly the same code(specifying file name at runtime) works in Netscape 7.0, while it doesn't work on other browsers(not even at Netscape 6). In IE 6 it opens Adobe Acrobat Reader, but than it throws the error "Can't open file,***.fdf"(*** keeps changing).I am able to view other pdf documents, from other sites on IE(so the problem is not in IE setting, i believe). I am attaching my code, I will be very thankful if anybody can help


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Good question. I have been dabbling myself in PDFs from Servlets but haven't had much luck or really time to get it to actually work. I was actually using XML with XSTL and XSL:FO with Apache's Fop, it says you can create PDFs dynamically without saving the actual PDF file.
It looks like you already have the PDF file on your Web server.
I have seen another product that I thought looked really good, but again you would be making the PDF on the fly. But that product was iText, I think that was the name of the product.

FOP has a Driver class that can be used to take a PDF file and send it to the Output stream.

Not sure fi that will help you in any particular way, but I thought I'd throw my 2 cents worth in.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Ryan Bailey
Ranch Hand

Joined: Feb 26, 2002
Posts: 134
XML with XSTL and XSL:FO with Apache's Fop works great for creating PDF files on the fly. I will be using that in a production environment. Using XSL:FO is great when you need to apply a stylesheet to an xml file. Are you dealing with xml? If not, then using XSL:FO might be an over kill since you have to parse the xml file, and then run it through Apache's FOP processer. I am storing the file but as Mark mentioned there is a while to generate an OutputStream and send that stream to the response.
I ran into a problem like this before. Try saving your results to a ByteArrayOutputStream and sending that to the OutputStream. For some reason bytes were being lost when I did it any other way.
Good luck,

Java: The power, elegance, and simplicity of a hand grenade
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
I wrote this article about PDF's, Java servlets, and iText
I agree. Here's the link:
subject: Displaying PDF files on browser
It's not a secret anymore!