• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in opening PDF file in client browser

 
Thanuja Vishwanath
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35269
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thanuja Vishwanath
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic