I’m having trouble displaying a PDF file in my app’s WebView. The PDF is stored on Google Drive, and I want to use Google Docs to render it.
What I tried:
- Downloaded the PDF from an external source
- Uploaded it to my Google Drive account
- Generated a shareable link for the document
- Attempted to load it in WebView through Google Docs viewer
- It didn’t work as expected
The issue:
When I use just the Google Drive sharing URL, it opens in the device browser instead of staying in my app. When I try to embed it using the Google Docs viewer format, the PDF doesn’t display properly and shows an error page instead.
Different URL formats I tested:
- Direct Google Drive share link
- Google Docs embedded viewer with the Drive URL
- Various combinations of parameters
None of these approaches successfully display the PDF content within my WebView. The document either redirects to an external browser or fails to load entirely.
What’s the correct way to display a PDF stored in Google Drive inside a WebView component? Is there a specific URL format or parameter I’m missing?
had the same issue a while back. make sure ur file is public, then try this format: https://drive.google.com/file/d/YOUR_FILE_ID/preview. it worked for me after a few fails with share links. also, check if javascript is enabled in your webview.
Google Drive URLs need authentication headers that WebView can’t handle. Don’t use the regular sharing URL - grab the file ID from your Drive link and use this format instead: https://docs.google.com/viewer?url=https://drive.google.com/uc?id=FILE_ID&export=download. This skips the Drive interface completely. Set your file permissions to “Anyone with the link can view” or you’ll hit access issues. I fought with this same problem for hours last month and this fix sorted it out.
The authentication redirect is what’s breaking your external browser. I ran into this same issue - Google Docs viewer works way better than embedding Drive links directly. Here’s what fixed it for me: grab your file ID from the Drive URL, then use https://docs.google.com/viewer?embedded=true&url=https://drive.google.com/uc?export=download&id=YOUR_FILE_ID. The embedded parameter keeps everything in your WebView instead of jumping to the browser. Quick heads up - PDFs with complex formatting or security restrictions can look wonky in Google viewer, so test with a basic PDF first to make sure it’s not a document problem.