Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a PDF file in an <iframe>?

I want to open the pdf file in an iframe. I am using following code:

<a class="iframeLink" href="https://something.com/HTC_One_XL_User_Guide.pdf"> User guide </a> 

It is opening fine in Firefox, but it is not opening in IE8.

Does anyone know how to make it work also for IE ?

like image 718
user1753210 Avatar asked Oct 19 '12 12:10

user1753210


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.

How do I open a PDF file in HTML?

To embed the PDF in the HTML window, point the page to a document and then specify the height and width of the PDF so the HTML window is the correct size using the code: <embed src="filename. pdf" width="500" height="375">. Note that an embedded PDF may look very different on different browsers and operating systems.

How do I embed a PDF in a Web page?

One popular option is that you upload the PDF file to an online storage service, something like Google Drive or Microsoft's OneDrive, make the file public and then copy-paste the IFRAME code provided by these services to quickly embed the document in any website.

How do I open an embedded PDF attachment?

To view the attachments, click on the paper clip in the upper right hand corner. 2) This will open up a pane on the left side of the screen with all the attachments. Double click on the attachment to open it. some versions, to get the paper clip, click on View → Navigation Panes → Attachments.)


2 Answers

Using an iframe to "render" a PDF will not work on all browsers; it depends on how the browser handles PDF files. Some browsers (such as Firefox and Chrome) have a built-in PDF rendered which allows them to display the PDF inline where as some older browsers (perhaps older versions of IE attempt to download the file instead).

Instead, I recommend checking out PDFObject which is a Javascript library to embed PDFs in HTML files. It handles browser compatibility pretty well and will most likely work on IE8.

In your HTML, you could set up a div to display the PDFs:

<div id="pdfRenderer"></div> 

Then, you can have Javascript code to embed a PDF in that div:

var pdf = new PDFObject({   url: "https://something.com/HTC_One_XL_User_Guide.pdf",   id: "pdfRendered",   pdfOpenParams: {     view: "FitH"   } }).embed("pdfRenderer"); 
like image 105
Aamir Avatar answered Sep 28 '22 08:09

Aamir


This is the code to link an HTTP(S) accessible PDF from an <iframe>:

<iframe src="https://research.google.com/pubs/archive/44678.pdf"    width="800" height="600"> 

Fiddle: http://jsfiddle.net/cEuZ3/1545/

EDIT: and you can use Javascript, from the <a> tag (onclick event) to set iFrame' SRC attribute at run-time...

EDIT 2: Apparently, it is a bug (but there are workarounds):

PDF files do not open in Internet Explorer with Adobe Reader 10.0 - users get an empty gray screen. How can I fix this for my users?

like image 42
Andrea Ligios Avatar answered Sep 28 '22 08:09

Andrea Ligios