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.

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer i got the solutions. will post it in answer for further help.
@Mayur, I have posted the solution that worked for me here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.