aspose file tools*
The moose likes Servlets and the fly likes How to display pdf file in broswer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to display pdf file in broswer" Watch "How to display pdf file in broswer" New topic
Author

How to display pdf file in broswer

malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69
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

Joined: Nov 22, 2011
Posts: 2
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

Joined: Aug 30, 2010
Posts: 147

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.


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

Joined: May 13, 2011
Posts: 69
I have replaced

"attachment;" with "inline;"


but still it gives the download dialog...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69
So can you tell me what could be the good response that could be fed to the browser?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

You actually have to write the PDF to the response stream. Where's your code that does that?
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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


SCJP 6 [SCJP - Old is Gold]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16058
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

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.


No pain, No gain.
OCJP 1.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display pdf file in broswer