2

In my application i am sending a path to the server and which returns me a pdf file contents in base64 string. Now i want to show this string in external viewer (viewer.html) using pdf.js. I am doing in this in following way, what wrong am i doing?

var res="JVBERi0xLjUK..."; // Base64 String from response shortened
var pdfData = base64ToUint8Array(res);
window.open('pdfJs/viewer.html?file='+pdfData);

function base64ToUint8Array(base64) {
var raw = atob(base64); //This is a native function that decodes a base64-encoded string.
var uint8Array = new Uint8Array(new ArrayBuffer(raw.length));
for(var i = 0; i < raw.length; i++) {
    uint8Array[i] = raw.charCodeAt(i);
}

return uint8Array;
}

Output i am getting now:

enter image description here

I have gone through some of answers but cant sort it out. So please help me.

Mayur
  • 1,123
  • 3
  • 22
  • 38

1 Answers1

5

The Uint8Array cannot be passed via query string. Use PDFViewerApplication.open() method on the opened window, e.g.

var w = window.open('web/viewer.html?file=');
w.addEventListener('load', function () {

  var res="JVBERi0xLjUK..."; // Base64 String from response shortened

  var pdfData = base64ToUint8Array(res);
  w.PDFViewerApplication.open(pdfData);

  function base64ToUint8Array(base64) {
    var raw = atob(base64);
    var uint8Array = new Uint8Array(raw.length);
    for(var i = 0; i < raw.length; i++) {
      uint8Array[i] = raw.charCodeAt(i);
    }
    return uint8Array;
  }
}, true);
async5
  • 2,505
  • 1
  • 20
  • 27
  • Thanks for your answer i got the solutions. will post it in answer for further help. – Mayur Feb 12 '15 at 10:53
  • @Mayur, I have posted the solution that worked for me [here](http://stackoverflow.com/questions/24535799/pdf-js-and-viewer-js-pass-a-stream-or-blob-to-the-viewer/32634740#32634740) – toddmo Sep 17 '15 at 15:58