How to display PDF from Google Drive in iframe without redirect issues

I’m working on a web application where I need to show PDF files inside an iframe. I upload these PDFs to Google Drive using a Python script with the Google API libraries. After uploading, I get a document URL that looks like this:

# Example of getting document URL after upload
doc_response = drive_service.upload_file(pdf_data)
document_url = doc_response.get_viewer_link()
print(document_url)
# Output: 'https://drive.google.com/file/d/1a2b3c4d5e6f7g8h9/view'

The problem is when I try to embed this URL in an iframe on my website, the PDF viewer keeps redirecting and breaks out of the iframe container. This makes it impossible to keep users on my site while they view the documents. I need a way to embed the PDF viewer properly so it stays within the iframe boundaries. Has anyone found a workable solution for embedding Google Drive PDFs without the redirect behavior? I’m open to alternative approaches as long as the documents remain hosted on Google’s servers.

Been dealing with this for years. The URL tricks mentioned above work sometimes, but Google’s cracking down on iframe embedding. I’ve had better luck with this format: https://docs.google.com/gview?url=https://drive.google.com/uc?id=YOUR_FILE_ID&embedded=true. The trick is wrapping the direct download link (/uc?id=) in Google’s document viewer. Just make sure your files are public and test in different browsers - corporate firewalls love blocking these. Heads up: larger PDFs can timeout with this method.

Google intentionally blocks iframe embedding using X-Frame-Options headers, which is why you experience the redirects. However, you can often bypass this issue by replacing /view with /preview in your Google Drive URL. An effective approach is to use the embed format: https://drive.google.com/file/d/YOUR_FILE_ID/preview?usp=embed. Ensure your PDF’s sharing settings are configured to ‘Anyone with the link can view’ to enable proper embedding. Note that Google’s support for this method is not guaranteed, and while it works for standard PDFs, interactive documents may still face issues.

Had the same prob last month! Just swap /view to /embed in your iframe src - totally fixed it for me. Don’t forget to add sandbox="allow-scripts allow-same-origin" to your iframe tag to avoid the redirect issues.