The moose likes Servlets and the fly likes Generating PDF from Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Generating PDF from Servlets" Watch "Generating PDF from Servlets" New topic

Generating PDF from Servlets

Sandeh Nivrutti

Joined: Jan 09, 2002
Posts: 4
I am trying to generate some pdf reports using a third party API (from When I run the servlet and view it in the browser, I get the output in some junk format. I think it is because the browser doesn't recognize the file's MIME type. Can you please suggest a work-around for this.
The url of the code I am using is :
A brief code snippet of the doGet() method of the servlet:
PdfPage page = new PdfPage(response.getOutputStream());
page.setFont(page.BOLD, 24);
page.println("Tutorial - Hello 2");
page.setFont(page.NORMAL, 12);
page.println(new Date().toString());

Pls let me know ASAP...
Balaji Loganathan
author and deputy

Joined: Jul 13, 2001
Posts: 3150
Two ways
1.try adding the line setHeader
response.setHeader("Content-Disposition","inline;filename=san.pdf ");

2. Call the servlet with .pdf extension by mapping servlet using web.xml
<display-name>Display PDF Servlet</display-name>
this two ways may tell the browser that you are opening/viewing a pdf file.

Spritle Software Blogs
Albert Ross

Joined: May 30, 2003
Posts: 2
What browser are you using? If it's IE5 or 5.5 then it has a problem displaying PDF files. You need to setContentLength() on the HttpServletResponse object to the size of the PDF file before you send it to the browser. I had similair problems outputting PDF files using Apache FOP but setting the content length sorted things out.
Sandeh Nivrutti

Joined: Jan 09, 2002
Posts: 4
Yes, you were right I am using IE 5.5. You suggested setting the content length of the response object to the size of the PDF document that I would be generating. But the problem, is that I want to generate PDF data on the fly, by displaying the resultset from the database, which is likely to vary and hence I cannot determine the document size before hand. So what is the alternative....???
One more thing, would it be better to display the PDF report on the browser directly or to save it to some temporary storage, from where the user can view it at his convenience?
John Dunn
Ranch Hand

Joined: Jan 30, 2003
Posts: 1108
To get the size try using a filter to capture the response then getting the size before sending it on: Take a look at these links:

GZIP filter
In the above case the GZIP needs to be done first so the size can be determined. So we filter it and capture output in a wrapper, then find size and send to browser. You'll need to do something similar.
Hope this points you in right direction...

"No one appreciates the very special genius of your conversation as the dog does."
Albert Ross

Joined: May 30, 2003
Posts: 2
I don't know exactly how you would do it with the renderer that you are using, but the advice I was given with Apache FOP was as follows.
1) Set the output stream from your renderer to a ByteArrayOutputStream so that you store your PDF file in memory.
2) Use ByteArrayOutputStream.size() to set the content length
3) Get the ServletOutputStream using response.getOutputStream
4) Use ByteArrayOutputStream.writeTo() to write the contents of your PDF to the browser.
Hope that this helps
Ravi Kumar Ravuru
Ranch Hand

Joined: Apr 18, 2002
Posts: 176
Please look into the following link for PDF generation;
there are lot of tutorials related to it.Hope this helps you.
I agree. Here's the link:
subject: Generating PDF from Servlets
It's not a secret anymore!