This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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());
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.
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
Joined: Jan 09, 2002
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?
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."
Joined: May 30, 2003
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