Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render Uint8Array throw error #110

Closed
Clebiez opened this issue Mar 8, 2016 · 6 comments
Closed

Render Uint8Array throw error #110

Clebiez opened this issue Mar 8, 2016 · 6 comments

Comments

@Clebiez
Copy link

Clebiez commented Mar 8, 2016

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 !

@sayanee
Copy link
Owner

sayanee commented Mar 24, 2016

Hi! Have you tried this form the documentation? Closing, but feel free to re-open if this doesn't work.

@sayanee sayanee closed this as completed Mar 24, 2016
@johnjspiteri
Copy link

johnjspiteri commented Dec 28, 2016

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);

@yogendrakarekar
Copy link

@johnjspiteri with your code provided above it does creates a pdf but when opened in browser shows no data.
This is the url generated below
blob:http://localhost:8888/be9be94b-5750-418c-9159-43d62c0657ad

@johnjspiteri
Copy link

I would love to help a fellow coder - would need more information. It can be difficult to create an online example...

It seems to me that the issue is browser related - can you test FireFox, Chrome and Safari to see if the PDF works on any of those browsers?

@jjroth
Copy link

jjroth commented Jan 29, 2020

@johnjspiteri You sir are a legend! That solution works brilliantly.

I've been searching the interwebs for days looking for a solution to this issue. You'd think being able to generate a downloadable pdf document from a base64 encoded string would be straightforward...but no!

This solution needs to be published far and wide to correct a lot of the misinformation out there.

Thanks so much for taking the time to post that solution!

@johnjspiteri
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants