Open pdf.js with a data URL in IE11

7k views Asked by At

I'm trying to use pdf.js for showing a document in a data URL on IE10. That is, something like this.

It works well with Firefox or Chrome, but in Internet Explorer 10 and 11, the interface is shown, but stays empty, and the document never loads.

I checked, compatibility.js is included in the renderer page (viewer.html), so IE support should be present.

EDIT: actually it's a security issue, as IE doesn't let to run a request of this type.

Thanks in advance,

1

There are 1 answers

4
Eino Gourdin On BEST ANSWER

Ok, if someone runs to the same problem, I solved it by bypassing the lib's normal loading path, and as async5 advised, converting directly the data to a byte array.

That is, in viewer.js, add those lines under line 6856 :

  if (file && file.lastIndexOf('data:', 0) === 0) {
      // data: url-scheme. we will load those with direct conversion to byte array

      function convertDataURIToBinary(dataURI) {
          var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
          var base64 = dataURI.substring(base64Index);
          var raw = window.atob(base64);
          var rawLength = raw.length;
          var array = new Uint8Array(new ArrayBuffer(rawLength));

          for(var i = 0; i < rawLength; i++) {
              array[i] = raw.charCodeAt(i);
          }
          return array;
      }

      // PDFViewerApplication.setTitleUsingUrl(file);
      PDFViewerApplication.open(convertDataURIToBinary(file), 0);

      return;
  }

(the base64 to byte array code is the one posted by Codetoffel here)