Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF Viewer using object tag not working for Edge

I am trying to show a PDF document inline using the object tag - it works as expected in Chrome and Firefox but isn't working in Edge.

Sample Code:

  <object id="pdfObj" data="http://www.pdf995.com/samples/pdf.pdf" type="application/pdf" target="_parent">
  </object>

Plunker: http://plnkr.co/edit/wH9ECcwkx3vHFv43xTo5?p=preview

What's a good way to show the PDF viewer in Edge? Thanks for your help.

like image 420
F.S. Avatar asked Apr 19 '16 17:04

F.S.


2 Answers

I've faced a similar issue. Object tag behaves differently in IE and EDGE; the sever needs to accept the HEAD request made by the object tag (HEAD request : only in IE & EDGE) and give a response, then a get request is made to fetch the file. This is a limitation in Microsoft's browsers.

Edit 1: This worked for me (EDGE and IE too) if I add the object tag html with the url in the data attribute to the dom instead of setting the attribute value, using javascript, of object tag which is already on the dom. Please note that I've a endpoint flushing response, this updates the value of data attribute.

like image 60
Harish Mashetty Avatar answered Oct 16 '22 00:10

Harish Mashetty


I replaced the value of the type attribute with text/html.

<object id="pdfObj" data="http://www.pdf995.com/samples/pdf.pdf" type="text/html" target="_parent"></object>

This method works for Edge and IE11.
The response of the <object> request contains html :

<!doctype html>
<html>
    <body style='height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);'>
        <embed style='position:absolute; left: 0; top: 0;'width='100%' height='100%' src='about:blank' type='application/pdf' internalid='5BD5603FA794B387B2B97F624FB9ABDE'></embed>
    </body>
</html>

Edge and IE11 maybe check the response content and block it because it is not a pdf...
The pdf is in the <embed> of the response.

Set type to text/html in this situation works for me on this browsers :

  • Firefox
  • Chrome
  • IE11
  • Edge
  • Opera

I don't try on Safari.

I hope this answer has helped you and that my English did not bother you too much

like image 42
Caucorico Avatar answered Oct 15 '22 23:10

Caucorico