Can Google Docs Viewer show a PDF from a JavaScript blob without local storage?

I’ve got a JavaScript blob containing a PDF file. I know how to use Google Docs Viewer to show PDFs from web links. But I’m wondering if there’s a way to make it work with my blob data.

Here’s what I can do now:

let pdfLink = 'https://example.com/sample.pdf';
window.open(`https://docs.google.com/viewer?url=${pdfLink}`, '_blank');

This opens the PDF in Google Docs Viewer just fine. But how can I do something similar with my blob? I don’t want to save the file on my computer first.

Has anyone figured out a trick for this? It would be super helpful if there’s a way to stream the blob data or convert it somehow. Thanks for any ideas!

I’ve actually faced a similar challenge recently. Unfortunately, Google Docs Viewer doesn’t support direct blob input. It’s designed to work with URLs, not local data.

One workaround I found is to use a data URL. You can convert your blob to a base64-encoded data URL and use that. However, be aware that this method has limitations, especially with large files.

Another option is to use a different PDF viewer library that supports blob input directly. PDF.js is a good choice - it’s open-source and works well with blobs.

If you must use Google Docs Viewer, you’d need to temporarily upload the blob to a server and generate a URL. This adds complexity but might be necessary depending on your specific requirements.

hey there! i’ve run into this too. google docs viewer is picky bout blobs. have u tried using PDF.js? it handles blobs like a champ. no need for storage tricks. just load ur blob right in and boom, pdf on screen. might be worth a shot if ur stuck!

I’ve dealt with this issue in a project recently. While Google Docs Viewer is great for URLs, it’s not designed for blobs. Here’s what worked for me:

I ended up using PDF.js, which handles blobs directly. It’s not as sleek as Google Docs Viewer, but it gets the job done without needing to store the file anywhere.

If you’re set on using Google Docs Viewer, you could look into creating a temporary object URL from your blob. Something like:

let objectUrl = URL.createObjectURL(pdfBlob);
window.open(`https://docs.google.com/viewer?url=${encodeURIComponent(objectUrl)}`, '_blank');

Keep in mind this might not work perfectly, and you’ll need to revoke the URL when you’re done. It’s a bit hacky, but it might do the trick if you’re in a pinch.