Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering PDFs in ie inside the browser

I've looked at so many different SO posts about this today.

I have an application that needs to show PDF documents inside the browser. This application also needs to run in IE (11+).

Here's the thing: an iframe with a src works just fine. Something like:

<iframe src="www.myurl.com/thedocument"></iframe>

However, www.myurl.com/thedocument is now protected by oAuth. What this means is I need to request www.myurl.com/thedocument with the appropriate authorization header credentials.

This means (i think), that I have to request www.myurl.com/thedocument via ajax. The ajax request returns base64, or a byte[] containing the document.

IE doesn't support data URIs to render PDFs, so i can't just splat the response from my ajax request into the iframe.

So.. now I'm stuck.

Any ideas?

Thanks

like image 549
Jeff Avatar asked Nov 18 '15 19:11

Jeff


People also ask

How to read PDF in Internet Explorer?

How to Read PDF in IE (Internet Explorer) 1 Step 1: Open the Internet Explorer. 2 Step 2: Click on Tools and then select "Manage Add-ons". 3 Step 3: When a window on add-on types appears choose toolbars and extensions. Choose the option of all add-ons and then... 4 4. How to Read PDF in Safari More ...

How do I activate the add-ons in IE PDF viewer?

The IE PDF Viewer utilizes the Adobe Reader, Adobe Acrobat, or Foxit Reader add-ons. Activating the add-ons through the following steps can help you get started viewing your documents on the browser. Step 1: Open the Internet Explorer. Step 2: Click on Tools and then select "Manage Add-ons".

How do I control how PDFs open from a web page?

Each browser has its own settings to control how PDFs open from a web page. Acrobat and Acrobat Reader do not include a preference setting to open web-based PDFs. To change the display behavior, follow the instructions below for your browser, or see the browser documentation on how to control plug-ins or add-ons.

Can I open a PDF file in IE mode?

We currently run IE Mode for a legacy application, and need PDF links to open in the native Adobe PDF application. I have "Always Download PDF Files" enabled, and Adobe is the default application for opening PDF files.


1 Answers

One option would be to use PDF.js, a javascript library for rendering PDFs into an HTML5 canvas with support for IE10+. The library supports loading PDF data from a TypedArray (e.g. Uint8Array), that could be produced from the result of an ajax request.

I've prepared a short example here that displays a single page PDF stored in a base64-encoded binary. To avoid performing the base64 conversion, a TypedArray could also be retrieved directly from an XMLHttpRequest response:

function reqListener () {
    var byteArray = new Uint8Array(this.response);
    PDFJS.getDocument(byteArray).then(function(page) {
        // ....
    });
}

var req = new XMLHttpRequest();
req.addEventListener("load", reqListener);
req.responseType = "arraybuffer";
req.open("GET", "http://www.example.com/example.pdf");
req.send();

To support the features that you would expect in a native pdf viewer (printing etc.) the library includes an example viewer that you can adapt for your own purposes.

like image 136
tmw Avatar answered Nov 08 '22 08:11

tmw