47

I am probably missing something simple here, however i will ask anyway. I have made a link to open up a PDF file, however it opens up in the current tab rather than a new one. What code shall i use in HTML to open a new tab to read the PDF file.

<div class="footer_box_content">
    <div class="cleaner_h10"></div>
    <p>Many  thanks  to  everyone  who cleared snow and ice during the cold spell in February.
    Should Arctic conditions return, each block has a shovel and a jar of rock salt  to  clear  the  steps. 
    Please click more to read the full newsletter.</p>
    <div class="button_01"><a href="newsletter_01.pdf">Read more</a></div>
</div>
pb2q
  • 56,563
  • 18
  • 143
  • 144
MultiWizard
  • 629
  • 2
  • 6
  • 5

10 Answers10

63
<a href="newsletter_01.pdf" target="_blank">Read more</a>

Target _blank will force the browser to open it in a new window

Chris
  • 3,675
  • 20
  • 29
  • 4
    Nope. If the user has set their browser to do something different with PDF files (like download them) it won't open in a new tab. There's no way to guarantee a PDF to open in a new browser window. – Chuck Le Butt Jun 12 '16 at 21:33
  • @ChuckLeButt Where did I state that this is guaranteed to open the page in a new window? – Chris Feb 01 '17 at 11:38
  • 18
    Quote: `Target _blank will force the browser to open it in a new window`. That's simply not true. You cannot force a browser to open a PDF. It may simply be downloaded, depending on the user's settings, even with the target attribute set to "_blank". See my answer below. – Chuck Le Butt Feb 01 '17 at 15:49
  • Force != guaranteed :) – Chris Jan 18 '19 at 16:21
  • 3
    There is no way to force a browser to open a PDF in a new window. – Chuck Le Butt Jan 20 '19 at 12:18
  • @Chris How to open with the same window like popup? Thanks – Gem Jul 05 '19 at 12:15
56

As everyone else has pointed out, this can work:

<a href="newsletter_01.pdf" target="_blank">Read more</a> 

But what nobody has pointed out is that it's not guaranteed to work.

There is no way to force a user's browser to open a PDF file in a new tab. Depending on the user's browser settings, even with target="_blank" the browser may react the following ways:

  1. Ask for action
  2. Open it in Adobe Acrobat
  3. Simply download the file directly to their computer

Take a look at Firefox's settings, for example:

enter image description here

Chrome has a similar setting:

enter image description here

If the user has chosen to "Save File" in their browsers settings when encountering a PDF, there is no way you can override it.

Chuck Le Butt
  • 45,923
  • 58
  • 187
  • 280
2

You have to use target attribute

<a href="newsletter_01.pdf" target="_blank">
Nick
  • 4,146
  • 1
  • 18
  • 29
  • 1
    Minus one. Sorry, this is not a forum. Suggest removing this post and upvoting the existing answer with this solution. – user66001 May 01 '21 at 15:53
  • @user66001 Could you please take into consideration difference in post time between the two posts? They are like posted in same minute. – Nick May 03 '21 at 01:01
  • I don't recall mentioning that this post shouldn't have ever existed, just now there it is a duplicate with one that has slightly more content, more comments to it discussing the solution, and far more upvotes (which means it will likely be the only one a portion of visitors to this page will see, hence the rest of the answers, especially duplicates, are just noise) the preference should be that the other one exist. – user66001 May 03 '21 at 04:17
2

Just use target on your tag <a>

<a href="newsletter_01.pdf" target="_blank">Read more</a>

The target attribute specifies where to open the link. Using "_blank" will make your browser to open a new window/tab.

You could also use target in many ways. See http://www.w3schools.com/tags/att_a_target.asp

Diogo Moreira
  • 1,022
  • 2
  • 9
  • 21
2

An observation, _blank alone will not make sure the file is opened in a new tab. The response header for the file request should have Content-Type : application/pdf.

Try this link, it will open the file in new tab as the server provide required headers.

Same is the case for other file types also. For ex: video files get request should have proper Content-Type like video/mp4.

JEWEL JACOB
  • 374
  • 3
  • 7
  • 1
    This helped me, I had response header coming as `Content-Type: application/octet-stream` which was downloading the pdf automatically that I didn’t want. `Content-Type : application/pdf` is what I needed it to be. – faruk13 Nov 23 '21 at 07:43
  • "Try this link, it will open the file in new tab"... Not for me in FireFox. It's still opening in the same tab. – dustytrash Jan 12 '22 at 14:39
  • This 'link' does not open in a tab for me; how do you apply the Content-Type to this HTML content? – J-Dizzle Jan 24 '22 at 17:24
  • Every file served by a server will have a Content-Type header. It will be either manually set as per need or derived form file type. And link not opening in new tab in all browsers, I assume there's browser specific settings for file types. In chrome desktop it actually opens in a new tab. – JEWEL JACOB Jan 26 '22 at 08:33
1

Change the <a> tag like this:

<a href="newsletter_01.pdf" target="_blank">

You can find more about the target attribute here.

gkalpak
  • 47,117
  • 8
  • 102
  • 117
  • 1
    Minus one. Sorry, this is not a forum. Suggest removing this post and upvoting the existing answer with this solution. – user66001 May 03 '21 at 07:44
1

On Chrome this has proven to work well for me.

<a href="newsletter_01.pdf" target="_new">Read more</a>
Fastersixth
  • 115
  • 1
  • 3
  • 13
0

Try this, it worked for me.

<td><a href="Docs/Chapter 1_ORG.pdf" target="pdf-frame">Chapter-1 Organizational</a></td>

-3

Will open your pdf in a new tab with pdf viewer and can download too

<a className=""
   href="/project_path_to_your_pdf_asset/failename.pdf"
   target="_blank"
>
   View PDF
</a>
  • 1
    Minus one. Sorry, this is not a forum. Suggest removing this post and upvoting the existing answer with this solution. – user66001 May 03 '21 at 07:43
-4

Use the below attribute in tag to open it in next tab

target="_blank"
Waqas Khan
  • 247
  • 1
  • 3
  • 16
  • 1
    Minus one. Sorry, this is not a forum. Suggest removing this post and upvoting the existing answer with this solution. – user66001 May 03 '21 at 07:43