• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generate PDF report from jsp

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
From a jsp page I click to view pdf report. In the server side I get the binary stream for the pdf from another application. Now I need to log this activity to another server using javascript. Since I have to do so. I dont directly send the response back as content type pdf. Instead I keep the stream in forward to a jsp.

From the jsp I need to create a pdf with the open save dialouge box instead of going to the server again.

Any help is appreciated.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please let us know how far you have gone and where are you stuck.
If this is a homework question then DoYourOwnHomework.

Then if you face any problem let us know.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I had been checking if there is an option to do so. In some pages I have seen I cant do this from a jsp. I want to know abt if this is possible.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doesn't sound like homework to me, but I don't really understand what is being asked. E.g., I can't make sense of "Now I need to log this activity to another server using javascript" at all. Can you elaborate what that means?

The part I did understand was to create a PDF version of a web page. I'd suggest to create the web page as XHTML styled with CSS; then you could use the Flying Saucer library.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank you. I will ellaborate on the issue I am having. From the server I get the pdf contents as binary stream. I could have directly written it outputstream to get the pdf. (This work fine also).

But I have an additional requirment that I need to log the details of the pdf (like name user etc) . This I have to do using Webtrend. They give us some javascript fucntions that sets certain varaibles onload of a jsp and then request for an image to the logging server(Webtrend). This passes all the details to the server.

Since I cant send the script along with the pdf sicne content type is pdf(wont be detected) what I am doing is forwarding the request to a jsp so that the script is run. Now in the session i had keep the contents of binary stream. From the jsp, can I use this binary stream to open a PDF with a dialouge box open read etc.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the JSP page generates the HTML that includes the JavaScript code. You can only either generate HTML or PDF - not both. But the JSP could automatically redirect to a servlet that streams the PDF, after the JavaScript code has sent the request for the image.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I didnt want a call to go again to the server side. But it seems I dont have a choice. Thanks a lot for the reply.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also make it easier by sending the notification to the logging service from the server (the java.net.URL class makes this rather easy), especially since you don't need to read the response.

That would also be more accurate, since the browser may be using something like AdBlock to suppress calls to that logging service.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I had thought of this but if invole the post request using java.net.URL, I wont be able to set certain headers. The javascript that webtrend (vendor provided) gives sets lot of information based on cookies and details of the browser or client. So I will not be able to set that details from the servlet part right. That is why I moved to the jsp solution.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In between I had put another post "javascript and jsp forward" from where I was redirected here. The one I have mentioned in this post is my current issue.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Since no one has replied back, I think there is a confusion about the issue I am having now. So putting it below:

This is currently what I try to do.

I have a parent submit jsp from where I submit to a servlet(to generate a report). From this servlet if the report type is pdf(content type is pdf), I forward to a intermediate jsp. The purpose of this jsp is to set certain headers and add the javascript content to html(this isto create a webtrend log req an image to server onload of page.). After running this script, I forward it to another servlet to write back the pdf response.

The issue is I cant write to the client before the forward. So I thought of opening a new explorer using a script and run the add javascript through that browser so that the intermediate jsp can go ahead with the forward.

First of all I am not sure if this works because when I try, I invoke the script onload but the control goes to the servlet with the forward and doesnt reach the script. If forward is there cant we do any thing else in the page to send to client
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't fully understand the approach you're trying, but it doesn't sound workable. After the server sent back the JSP with the embedded JavaScript, the current request is done - the server can't generate anything else, or forward to some other place.

You really need to have the HTML page you sent back refresh itself to display the final URL. This is done by calling "document.href.url = '...'" in JavaScript. You said before that you didn't want to call the server again, but I don't really see what's the big deal about that.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can I use Frameset to have to part one with content type as html/text and the other to have content type as pdf
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, but then you'd have 3 requests instead of 2. Plus, frames cause other problems, and have really gone out of fashion.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it help if you embed the pdf in your HTML jsp:

<object data="http://here.is.my/document.pdf" width="100%" height="600" type="application/pdf"><a href="http://here.is.my/document.pdf">some text</a></object>

This hybrid solution allows you to show the pdf, but you can set output content to "text/html".
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I cannot try the file as link, as I must not change the current design.

Currently I am trying what Ulf Dittmer suggested. I show a jsp that will run the script(with a message like your report is generated). Onload of the page I set the url to the handler. But here also I am having issues.

window.location.replace().This is supposed to rewrite the current docuemnt link in the history also. but this doesnt happens, so when I click browser back after opening the pdf(opening works fine), it brings me back to the script running jsp.(eventhough replace needs to overwrite that link from history).

So I tried to submit to the same link again and in the servlet side I am checking if it is a second refresh(setting variable in session and removing it), go to handler to write the pdf stream,now pdf is considered as text by the browser and the pdf is directly writen to page as text inspite of my setting the res.setContentType("application/pdf").


I have removed the meta header from the reloading jsp still no effect. Since it is a refresh, does the browser consider the content type as that of the old.

Can you please help me on this...

Thanks
Sree Lakshmi
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

out.println(report.getContentString().toString());


This won't work. A PDF is binary data, you can't treat it as text. That means you can't use JSPs to stream a PDF; you'll need to do this in a servlet.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am doing this in a servlet (handler)only. Also this works fine, if I direct the request directly to this handler. If the request is a window.location.reload(true), then only the issue happens.

Currently from the submit jsp(J1)(where user selects the select report), it goes to the handler(H1), where it gets the report from SAS.So if the report type is pdf or excel, we forward to temporary jsp, which has the scripts added and run and onload of this page the reload() is called for the same handler(H1)(to avoid the back url issue). In the handler, I see if the req has any hidden value set, if set, it is directed to report displaying handler(H2) that has the code given in the previous reply.

So I dont get it why does it displays it as text.

If I directly forward the req on submit(J1) to H2, the report is displayed as pdf . So is it because it is like a refresh.

Can we do something that the browser comes to know it is pdf
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

res.setHeader("Content-disposition","inline; filename=lf.pdf");


This also looks suspicious. The header is called "Content-Disposition", not "Content-disposition", and if you want it to be saved it needs to be "attachment", not "inline". If it's supposed to be displayed, remove the filename.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply, I have changed the same but that doesnt bring any effect.

The issue is the pdf content is not recoginized by the browser as pdf. The browser displays as below. That means it is taking ti as text and not pdf.

If I send the request directly to handler instead of refresh, the pdf is shown correctly, so the header are fine I guess. Is it because of the refresh and the previous page was text this time also browser thinks it to be text. I dont get any message about the content type has been reset or something.


[ UD: added line breaks ]
[ August 15, 2008: Message edited by: Ulf Dittmer ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you streaming the PDF file to the client? As I pointed out before, it needs to be treated as binary data, not text, or there are bound to be problems, no matter if it works in some circumstances.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Finally one solution seems to work. Instead of reloading the url using window.location.reload(), I am submitting the form on load of the intermediate jsp, this brings the report in correct format.

In order to handle the back button issue because of a new request due to submit, I give the submit of form to the same url. So it will be considered as refresh by the browser. At the handler side, I handle he logic based on if req is coming for first or second time.

I have to do a lot of testing to confirm this. This doesnt seems to be a good way of doing but dont have any other option.

Thank you very much for the help. The url reload idea lead to this solution. Will be letting you know the result soon.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Thread hi-jack removed. Please start your own questions in a new post.]
[ September 08, 2008: Message edited by: Bear Bibeault ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic