aspose file tools*
The moose likes Servlets and the fly likes Problem in opening PDF file in client browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in opening PDF file in client browser" Watch "Problem in opening PDF file in client browser" New topic
Author

Problem in opening PDF file in client browser

Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Hi,

Iam using iText to create PDF. once the PDF file is constructed and created in server it will be opened in client broswer for client to view or save. Following code tries to create PDF and open the file in client broswer or save it. But the below code creates a PDF file with data constructed. After that created PDF has to open. Iam not able to open the created PDF. Please help.



When i used


i was able to open the PDF file. But problem is its not opened in client machine rather it opens in server itself as Runtime.getRuntime does the same.

So please help me in resolving this.


Thanks,
Thanuja.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
233

This code tells iText to create the PDF in a physical file.


Since you want it streamed to the browser, can you think of an OutputStream that the servlet knows about? (this is a hint that should point you towards the answer)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Also note that setting the Content-Length header to the exact pdf file size may be required to make the browser happy.

Bill
Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Jeanne Boyarsky wrote:This code tells iText to create the PDF in a physical file.


Since you want it streamed to the browser, can you think of an OutputStream that the servlet knows about? (this is a hint that should point you towards the answer)


Iam sorry.......actually that line needs to be commented. Previously i created file in the server and used Runttime to open the file. As i already mentioned the generated file is opened in server and not in the client machine. So that line needs to be commented.

Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Thanuja Vishwanath wrote:

Iam sorry.......actually that line needs to be commented. Previously i created file in the server and used Runttime to open the file. As i already mentioned the generated file is opened in server and not in the client machine. So that line needs to be commented.

Can you give us the the updated code that looks like what you really want to do then?

Remember that the code should not use java.io.File anywhere(!) since you are not writing anything to file, you are only writing to stream.
Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Ilari Moilanen wrote:
Thanuja Vishwanath wrote:

Iam sorry.......actually that line needs to be commented. Previously i created file in the server and used Runttime to open the file. As i already mentioned the generated file is opened in server and not in the client machine. So that line needs to be commented.

Can you give us the the updated code that looks like what you really want to do then?

Remember that the code should not use java.io.File anywhere(!) since you are not writing anything to file, you are only writing to stream.


This is my updated code.

Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
I do not see any major problems in your code but there must be if it does not work...

But you do not need the temporary outputstream, you can use directly the stream you are actually writing to.

See for example
http://www.geek-tutorials.com/java/itext/servlet_jsp_output_pdf.php
Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Ilari Moilanen wrote:I do not see any major problems in your code but there must be if it does not work...

But you do not need the temporary outputstream, you can use directly the stream you are actually writing to.

See for example
http://www.geek-tutorials.com/java/itext/servlet_jsp_output_pdf.php


Thanks for the link. I tried the example given in that link. huh not sure what the issue is even that is not working. find below the example code given in the link . Iam using IE8 and chrome. Could that be browser issue. Can anyone tell me if the below code works for you in IE8 or chrome?


Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Ok atlast iam able to generate PDF. Problem is when i compile the above servlet file and run it is working. But in actual iam calling this servlet from a JSP. I have a JSP where i have a link called EXPORT TO PDF.

when i click that link it calls a javascript method called createPDF(poNum). Please find the below javascript function. This javascript function inturn calls the servlet PoPdfServlet which generates PDF. Now my problem is, servlet generates PDF if i compile and run it. But when i call that from JSP it is not generating. So i guess iam missing something in this javascript or JSP.

In JSP i have a link


Javascript



and my web.xml

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Thanuja Vishwanath wrote:
Javascript





I don't think you need to use AJAX here, and in fact, I suspect that is why it isn't working. In the above code, it looks like you're attempting to load the PDF data into the "str" javascript variable, which won't work, and isn't really what you want to do. You really want the browser to retrieve the link, and determine what to do with a PDF file (it's much better at this than JavaScript would be).

Try something like this instead:



Depending on the user's settings, they will either be prompted for what to do with a PDF, or if they use the Adobe plugin, the plugin should open & display the PDF in the browser.

OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49

Yes Perfect Nelson. It works perfect. Thanks a lot. I removed Ajax and it works. Instead of document.location i used window.open(getParams,'_blank')

It works as required. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in opening PDF file in client browser