Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Response as PDF

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
M Jiva
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code I normally use, when I need to show some dynamic content - like a PDF
 
Saloon Keeper
Posts: 28054
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
M Jiva
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ??
 
reply
    Bookmark Topic Watch Topic
  • New Topic