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

Can we pass the base 64 blob? #85

Closed
rchancey opened this issue Mar 27, 2020 · 5 comments
Closed

Can we pass the base 64 blob? #85

rchancey opened this issue Mar 27, 2020 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@rchancey
Copy link

Can we use the actual pdf data blob rather than a URL?

@phuocng phuocng added the documentation Improvements or additions to documentation label Mar 28, 2020
@phuocng
Copy link
Member

phuocng commented Mar 28, 2020

It's possible @rchancey (as you see it supports opening a local file).
I'll try to create a live demo.

@phuocng
Copy link
Member

phuocng commented Mar 28, 2020

@rchancey
Here is the live example.

The Viewer's url could be a String or Uint8Array (which you can convert from blob to), but currently it's set as String.
I'll create another PR to update the type definition.

@phuocng
Copy link
Member

phuocng commented Mar 29, 2020

@rchancey
You can convert base64 to Blob, and create a custom URL with URL.createObjectURL(blob):

const base64toBlob = (b64Data, contentType='', sliceSize=512) => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);

    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }

  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}
  • Create custom URL from the Blob
const blob = base64toBlob(b64Data, contentType);
const blobUrl = URL.createObjectURL(blob);
  • Finally, pass the blobUrl to Viewer:
<Viewer fileUrl={blobUrl} />

@phuocng
Copy link
Member

phuocng commented Mar 29, 2020

@phuocng
Copy link
Member

phuocng commented Apr 10, 2020

Done.

@phuocng phuocng closed this as completed Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants