Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening PDF from within iframe in new tab - Google Chrome

I have a web page with an embedded iframe (iframe contents hosted under the same domain). Inside that iframe there is a link that is supposed to open a PDF file in a new tab when clicked.

iframe inclusion HTML:

<iframe src="./pages/ins_and_outs/ins_and_outs.html" width="100%" height="800"  frameborder="0" id="ins-and-outs" target="ins-and-outs" sandbox="allow-scripts allow-top-navigation allow-popups"></iframe>

iframe:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
...
<a href="./ins-and-outs/downloads/ins-and-outs.pdf" target="_blank">Open our infographic</a>
...
</html>
  • Chrome version: 52.0.2743.116 (64-bit)
  • OS: OSX 10.11.2

Upon clicking the link to the PDF in Google Chrome, it opens a new tab showing a blank page but with the PDF's URL in the address bar.

I first thought it was an issue with the PDF itself, so I manually copy pasted its URL in the browser's address bar but found it displayed without issues.

I then went on and tested my page in Firefox, clicking the link there resulted in the PDF displaying correctly in a new tab.

I've also tried placing the link outside the iframe for test purposes, which works as well.

My conclusion is that it's a specific issue with having a PDF open in a new tab by clicking a link in an iframe, and only in Google Chrome.

Any ideas to what exactly is causing this, and what kind of solution would be possible?

What I'm doing as a quickfix now is adding the "download" attribute to the link, so it no longer displays the PDF but just downloads it.

<a href="./ins-and-outs/downloads/ins-and-outs.pdf" target="_blank" download>Open our infographic</a>
like image 325
Jones03 Avatar asked Aug 17 '16 06:08

Jones03


People also ask

How do I open a PDF in iframe?

All you need to do is add #view=fitH to the end of the source attribute. That's it! fitH stands for fit horizontal, and this will make the PDF in our iframe fit the width of the screen.


1 Answers

I found something! I know your question was 2 years ago, but if you use the sandbox "allow-popups-to-escape-sandbox" in your iframe, this will work.

Here is an example:

<iframe id="blue" src="iframe.html" sandbox="allow-popups-to-escape-sandbox"></iframe>
like image 68
Matheus Cavallini Avatar answered Oct 08 '22 02:10

Matheus Cavallini