25

I have a href link to a pdf file that when clicked on it downloads. I want this to just open in a new page instead of downloading.

<a href="../file/1012">clicking here</a>

I've had a look at a website that does this with iframe, but I would prefer not to use that.

kurupt_89
  • 1,542
  • 8
  • 37
  • 65
  • 2
    Possible duplicate of [How to force files to open in browser instead of download (pdf)?](http://stackoverflow.com/questions/6293893/how-to-force-files-to-open-in-browser-instead-of-download-pdf) – Benjamin W. Dec 30 '16 at 06:14

5 Answers5

36

Your server should return this http header:

Content-Type: application/pdf

For viewing in browser it should also return:

Content-Disposition: inline;filename="myfile.pdf"

For downloading:

Content-Disposition: attachment;filename="myfile.pdf"
BornToCode
  • 9,495
  • 9
  • 66
  • 83
  • 1
    This is the answer! Thanks! – Alexandr May 24 '16 at 21:13
  • Can i add this both headers, `attachment` and `inline` in a single pdf ? . As i have 2 links one for view and another for download, i can't download the pdf as on clicking on download link it opens up in browser because of the added inline value. please let me know your suggestions on this . – huzefa biyawarwala Jul 15 '16 at 05:17
  • @huzefabiyawarwala - Simply create two different paths and send in each different headers. e.g. mysite.com/myfile.pdf & mysite.com/downloads/myfile.pdf – BornToCode Jul 15 '16 at 13:24
4

add target="_blank" to get a new window. adding .pdf may help. If you have control over the host web server adjusting the headers for the files in question will also help it open inline. I can tell you what headers to set if you are able too

kmcc049
  • 2,783
  • 17
  • 13
2

The server tells the browser whether the content you are getting is "inline" or not. If you are controlling the server, you will want to change the server's settings for whether to serve the problematic file as an attachment. Otherwise, there are browser plug-ins which allow you to override this. For Firefox, there is Open in Browser; http://spasche.net/openinbrowser/

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Here is a hack which lets the user control this; http://www.askapache.com/tools/pdf-cookies-headers-rewrites.html – tripleee Sep 01 '11 at 07:41
1

the answer by BornToCode is correct for "pdf".

for doc / docx / xls / xlsx / ...

and you don't want iframe, you can use:

<a href="http://docs.google.com/gview?url=https://sifr.in/img/292/1/courseAndroid.xlsx&embedded=true">Open your excel file</a>
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
-3

You can make the link open in a new window by adding target="_blank". The question if the PDF file is opened in the window or downloaded depends on the software the user has installed, i don't think it is possible to control that.

svanelten
  • 473
  • 2
  • 14