question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Render Uint8Array throw error

See original GitHub issue

Hi all,

I wan’t to render a base64 PDF file with your solution ! I convert base64 to Uint8Array and I try to render it with PDFJS like the following

    function convertDataURIToBinary(dataURI) {
      var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
      var base64 = dataURI.substring(base64Index);
      var raw = window.atob(base64File);
      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;
    }
    var pdfAsDataUri = "data:application/pdf;base64,JVBERi0xLjUN..."; //AWESOME LENGTH
    var BASE64_MARKER = ";base64,";
    $scope.pdf = convertDataURIToBinary(pdfAsDataUri);

But when I test it, I see the following warning :

Warning: Deprecated API usage: getDocument is called with pdfDataRangeTransport, passwordCallback or progressCallback argument

And the following error :

TypeError: url.charAt is not a function at combineUrl (pdf.js:333)

Thanks to your help !

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
johnjspitericommented, Jan 30, 2020

In case anyone needs a working version of the process:

var binaryImg = atob(base64String);
var binaryImgLength = binaryImg.length;
var arrayBuffer = new ArrayBuffer(binaryImgLength);
var uInt8Array = new Uint8Array(arrayBuffer);

for (var i = 0;  i < length;  i++) {
    uInt8Array[i] = binaryImg.charCodeAt(i);
}
var outputBlob = new Blob([uInt8Array], {type: 'application/pdf'});
$scope.pdfUrl = URL.createObjectURL(outputBlob);
0reactions
johnjspitericommented, Jan 30, 2020

@jjroth you are welcome! Happy that you are getting to where you are going… you inspired me to refactor the answer a little.

Read more comments on GitHub >

github_iconTop Results From Across the Web

render a Uint8Array pdf throw error · Issue #7070 · mozilla/pdf.js
No description provided. The text was updated successfully, but these errors were encountered: ...
Read more >
Javascript: Render PNG stored as Uint8Array ... - Stack Overflow
The proposed solution for turning your Uint8Array to base64 data will throw call stack size errors for large images.
Read more >
RangeError: invalid array length - JavaScript - MDN Web Docs
The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds ...
Read more >
JSDoc: Source: display/api.js
Primarily used to improve performance of font rendering, ... "object") {; throw new Error(; "Invalid parameter in getDocument, " +; "need either Uint8Array, ......
Read more >
React Bundle Problem (main[hast].js after build) - JavaScript
_(e))throw new Error("unsupported BodyInit type");this. ... Component { // do stuff } ReactDOM.render(<MyThing>, myElement); </script>.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found