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

Hey everyone,

I’ve got a question about using Google Docs Viewer with JavaScript blobs. Right now, I can easily show PDFs from web links using the viewer. It works great!

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

But here’s what I’m wondering: Is there a way to use Google Docs Viewer to display a PDF that’s stored as a JavaScript blob? The catch is, I don’t want to save the file on my computer or server first.

I’ve been scratching my head over this for a while now. If anyone has figured out a trick to do this or knows if it’s even possible, I’d love to hear about it! Thanks in advance for any help or ideas you can share.

I’ve encountered this issue in my work as well. While Google Docs Viewer doesn’t directly support blob viewing, there’s another approach you might consider. Instead of using Google Docs Viewer, you could utilize the browser’s built-in PDF viewer capabilities. Most modern browsers can render PDFs natively.

Here’s a method I’ve used successfully:

Create a Blob URL from your PDF blob, then embed it in an iframe or object tag. This approach keeps the PDF in-memory without saving it locally or on a server. Remember to revoke the Blob URL when you’re done to free up resources.

This method provides a seamless viewing experience without relying on external services. It’s been quite reliable in my projects, especially when dealing with dynamically generated PDFs.

I’ve actually tackled this problem before in a project. Unfortunately, Google Docs Viewer doesn’t support direct blob viewing without a URL. However, there’s a workaround you might find useful. You can create a temporary object URL from your blob using URL.createObjectURL(). This generates a local URL that you can then pass to the viewer. Just remember to revoke the URL when you’re done to free up memory. Here’s a quick example:

let pdfBlob = // your PDF blob
let blobUrl = URL.createObjectURL(pdfBlob);
window.open(`https://docs.google.com/viewer?url=${encodeURIComponent(blobUrl)}`, '_blank');
// Don't forget to revoke the URL when you're done
URL.revokeObjectURL(blobUrl);

This method avoids local storage while still leveraging Google Docs Viewer’s functionality. Hope this helps!

hey flyingstar, i’ve faced similar issues. sadly, google docs viewer needs a url to work. for blobs, you’d need to create a temporary url. have u considered using pdf.js? it can handle blobs directly without storage. might be worth checkin out if google docs viewer is a no-go.