• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How to display pdf file in broswer

 
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.

 
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+";");
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • 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...
 
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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?
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome 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
 
Saloon Keeper
Posts: 21598
146
Android Eclipse IDE Tomcat Server Redhat Java 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.
 
Ranch Hand
Posts: 171
Spring Java Linux
  • 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.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!