Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating PDF from Servlets

 
Sandeh Nivrutti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to generate some pdf reports using a third party API (from http://www.dot.state.oh.us). 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 : http://www.dot.state.oh.us/javapdf/javatopdf/doc/Tutorial/hello2.java.html
A brief code snippet of the doGet() method of the servlet:
response.setContentType("application/pdf");
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
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two ways
1.try adding the line setHeader
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","inline;filename=san.pdf ");

2. Call the servlet with .pdf extension by mapping servlet using web.xml
<servlet>
<servlet-name>san.pdf</servlet-name>
<display-name>Display PDF Servlet</display-name>
<servlet-class>hello2</servlet-class>
</servlet>
this two ways may tell the browser that you are opening/viewing a pdf file.
 
Albert Ross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards
Albert
 
Sandeh Nivrutti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Albert Ross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Regards
Albert
 
Ravi Kumar Ravuru
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Please look into the following link for PDF generation;
http://www.lowagie.com/iText/
there are lot of tutorials related to it.Hope this helps you.
Ravi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic