Embed Pdf from Google drive with webview

I had to modify my app a little bit to test this, since it’s designed to use the wrapping method exclusively if the url contains ‘PDF’, or ‘Drive’. Ultimately, it appears to be the same viewer regardless if you wrap the url, or append the preview.

One thing I noticed is that if you use the ‘/Preview’ method, then you have to manipulate the URL. It seems that I got the ‘cannot be embedded’ message if I directly appended ‘/preview’ to the end of the url, that I got from google drive when I retrieved the shared url. I had to remove the ‘view?usp=sharing’ part before appending ‘/preview’. So, with that, it does appear to be a little more involved to clean up the url before appending ‘/preview’ to get it to work with a webview component.

With that said, I did run a test with the same PDF, but I used my wrapping method with a PDF stored in glide storage and compared it to a PDF stored in google drive that used the preview method.

Using the preview method did appear to be much more reliable as far as consistently opening the viewer in a webview component every single time.
Using the wrapping method, I probably get a failure for the PDF to load 1 out of every 5 tries (completely closing the app after each try). I really don’t know if it’s something on my end with my old phone, or something on googles end because it has to retrieve the file from an outside source before displaying it in it’s own viewer. I’m guessing it’s a cache issue somewhere.

As a third test, I tried using the wrapping method with the pdf stored in drive and I started getting the html text issue that you had. Not sure why. I remember dealing with that a long time ago. I think this was a solution for me at the time. I had to use a different url.

As of right now, I don’t typically store files in google drive if I don’t have to. I continued testing, and I did get the webview to fail once or twice with a drive stored file and using the wrapping method.

I wouldn’t say that my tests are conclusive. I could probably poke at it all day with varying results depending on where the file is stored, and which method I used to display it in a web view. Keep in mind that my user base is only one or two users, so having to back up and reopen the webview is not a big deal for us. If you intend to store all of your files in google drive, then keep in mind google’s usage limitations and the fact that it’s not truly meant to be a web host for files. But if you do, then it might be worth setting up your table to have a column with the url wrapped using the same method I use and another column that appends the preview to the end. Test both and see which one works the best.

If you intend to use the viewer with pdf’s stored in glide or any other website except google drive, then I’m not aware of any other method that will allow you to nicely view a PDF inside of a webview component. The wrapping method was the only one that gave me the results I was happy with. If you find something else that works, I would be happy to now about it.

2 Likes