• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display pdf file in broswer

 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In my servlet I am using this code to open a pdf file in a browser,
but instead it shows a download dialog box.

Any help, what I am doing wrong...
Thanks.

 
Tech Raj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the line:
response.setHeader("Content-Disposition", "attachment; filename="+filepath+";");

else

Replace "attachment;" with "inline;" in

response.setHeader("Content-Disposition", "attachment; filename="+filepath+";");
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HTTP content-disposition header does two things - it suggests to the browser how to open the file, and what to name the file. When you specify "attachment", you're actually overriding the browser's default behavior and telling it to save the file instead.

If you want it to open in the browser, change this value to "inline".

Also, you really only want a file name, and not a full path for "filename" setting. This does not refer to the original location on the server, and unless the browser has a severe security hole, the browser should not save the file at that location on the client, either. You should strip the path information off of the "filepath" String before setting it as the "filename" value.
 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have replaced

"attachment;" with "inline;"


but still it gives the download dialog...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do actually write the contents of the file to the response? What's the purpose of the output stream instance? Why do you define the filepath variable if you are not going to use it?

If you feed a nonsense response to the browser, it's not surprising that it will not act as expected.
 
malik ge
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So can you tell me what could be the good response that could be fed to the browser?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You actually have to write the PDF to the response stream. Where's your code that does that?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear Bibeault said,

PDF content has to be written in the response stream.. here you are writing only to the local file.

As you send the response in form of "application/pdf', browser recognizes its pdf content and opens up the PDF in Adobe Reader...


have the "filename" with proper "Filename only" not the full path of the file.

"Inline" will work for sure... Please check your code, compile once again and try
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, just in case you had hopes of literally opening the PDF in a web browser window, that doesn't work under Microsoft Windows. There's a patent on that technique and Microsoft lost a lawsuit to the tune of half a billion dollars, so they can only invoke an external program such as Adobe Reader to open and display the PDF.

Linux was exempted from that restriction. Although considering what the Reader plugin has been displaying for me lately, that's not as wonderful as it seems. The plugin can get pretty corrupt, requiring a browser restart to reset itself.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "inline" works well for me quite long time without any issues, but as "Tim" mentioned IE will get open the response by a third party software like adope reader but same will open in browser window itself. Clients had raised issues on this but closed all since it's the behaviour of IE.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic