File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Response as PDF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Response as PDF" Watch "Response as PDF" New topic
Author

Response as PDF

M Jiva
Greenhorn

Joined: Sep 27, 2010
Posts: 8
hi
i want to sent the response output of my servlet to a the user as a pdf. like as the user selects a particular set of options, the servlet does some calculations and should send a pdf as an output to the user..
So basically,I want to generate PDF content to be served up directly through a servlet with no intermediary file in the file system..

I think we can do this using streams but i am not able to get my code correct.

Can anybody provide me with any example code where i can put my calculations done in the servlet in pdf format to the user.. i have gone through iText documentation and feel that iText is used to write the output to a pdf file but here i do not want to generate any intermediate file and want the output to be directly presented to the user in pdf format..

please help!!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
One of the key problems is just getting the response headers set correctly so the browser will know what to do.

What do you have working so far?

Are you able to test the PDF conversion by writing a file that reads correctly?


Bill
M Jiva
Greenhorn

Joined: Sep 27, 2010
Posts: 8
i have tried gnujpdf API's, and with that i am able to generate a pdf file carrying the outputs from the servlets. but that makes and saves a pdf file on my server machine, whereas i want to send the output directly to the client browser in the form of pdf file which the user can download. i dont want to create any intermediate pdf files on my server machine..

Here is the servlet code i am trying, i want to print things in pdf form..

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What code do you use for generating the PDF ??

I don't know the gnujpdf API - but by looking at the documentation I found this line

have you tried to use a ByteArrayOutputStream instead ??


Regards, Rene Larsen
Dropbox Invite
M Jiva
Greenhorn

Joined: Sep 27, 2010
Posts: 8
thanks for reply,
here is the code i m trying with ByteArrayOutputStream, but it does not opens any pdf in the browser, instead it says that "Internet Explorer cannot display the webpage"..

please review the code to see what is wrong..
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

This is the code I normally use, when I need to show some dynamic content - like a PDF
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

I don't know if this still applies, but some browsers could not handle PDF's unless the content length header was correctly set.

Since an on-the-fly PDF generation doesn't know how large it will be until long after the headers have been sent, that forced the need to create a temporary PDF file on the server and then spool it out in the response stream with a content-length header. Sorry about that.

You can minimize the annoyance, however. Use the java.io. makeTempFile method to create your SPOOL pdf file. There's also an option that allows the file to be automatically deleted after it is read, which leaves only the grunt work of copying from the SPOOL file to the HttpOutputStream.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I know that iText can be used to create PDFs in memory to be streamed to the browser.
This might be desirable for smaller PDFs but, for larger ones, you might prefer to stream them to a file and then stream that file to the browser.

I've been able to stream PDFs to current browsers without knowing the content length. In the app I'm working on, I don't need to support older browsers so I'm not sure which ones required the content length header. I do know that this issue is covered in "iText In Action" by Bruno Lowagie along with some workarounds.

Last note: the current version of iText requires you to either buy a commercial license or comply with the terms of the GNU Affero Public License. Something you should understand before releasing an app/site with this library.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
M Jiva
Greenhorn

Joined: Sep 27, 2010
Posts: 8
thanks for reply all of you, but i am still looking for code to place my output values in tabular format in a pdf format using streams. how to convert all my values in formatted output (i.e. in tabular format) into binary array and then push that binary array to the client browser.. (i hope the later part is explained in Rene Larsen's post)..

suppose i have this output to be shown as in pdf

serial no name roll number marks
1. XYZ 12345 95
2. PQR 67890 99
.
.
.
.
.
M Jiva
Greenhorn

Joined: Sep 27, 2010
Posts: 8
And yes i am also worried of the commercial license clause for close code application attached with iText, please suggest any generic way of doing this. but yes somebody suggested that it is possible to cache the pdf generated in the memory(using iText) and then stream the same to the client browser, can you post the code please..

Thank you all
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

M Jiva wrote:thanks for reply all of you, but i am still looking for code to place my output values in tabular format in a pdf format using streams. how to convert all my values in formatted output (i.e. in tabular format) into binary array and then push that binary array to the client browser.. (i hope the later part is explained in Rene Larsen's post)..

suppose i have this output to be shown as in pdf

serial no name roll number marks
1. XYZ 12345 95
2. PQR 67890 99
.
.
.
.
.

I don't think I understand your problem ??

If you have generated a PDF, and instead of writing it to a FileOutputStream you write it to a ByteArrayOutputStream - then you'll have the PDF formatted in eg. tabular format (or what ever format you choose).

Are you able to generate a PDF with the right formats using FileOutputStream ??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Response as PDF