Integrating Google Docs PDF viewer within an IFRAME

Hey everyone, I’m trying to show a PDF from Google Docs in an IFRAME on my website. I’ve uploaded the PDF using Python’s gdata library, and I can get a link to the document. The problem is, when I use this link in an IFRAME, it doesn’t work right. The PDF viewer keeps redirecting to itself and breaks out of the IFRAME.

I did some digging and found out about an embeddable Google Document viewer, which seems perfect. But I can’t figure out how to use it with PDFs I’ve uploaded to Google Docs. Is this even possible? Has anyone managed to do this before?

Here’s a simple example of what I’ve tried:

def get_pdf_link():
    doc = upload_to_google_docs('my_file.pdf')
    return doc.get_alternate_link().href

pdf_link = get_pdf_link()
iframe_html = f'<iframe src="{pdf_link}"></iframe>'

But this doesn’t work as expected. Any ideas on how to make this work or if there’s a better way to embed Google Docs PDFs? Thanks in advance for any help!

I’ve dealt with this exact problem before, and let me tell you, it can be a real headache. What ended up working for me was using the Google Drive API instead of the Docs API. It’s a bit more straightforward for handling PDFs.

First, you’ll need to authenticate with the Drive API. Then, when you upload the PDF, you can get the file ID directly. Here’s a rough idea of how it might look:

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

drive_service = build('drive', 'v3', credentials=creds)

file_metadata = {'name': 'my_file.pdf'}
media = MediaFileUpload('my_file.pdf', resumable=True)
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
file_id = file.get('id')

iframe_url = f'https://drive.google.com/file/d/{file_id}/preview'

This approach has been much more reliable for me. Just remember to set the appropriate sharing settings on the file after upload. Hope this helps!

have u tried using the google docs viewer url? it’s like this:

https://docs.google.com/viewer?url=YOUR_PDF_URL&embedded=true

replace YOUR_PDF_URL with the actual link to ur pdf. this might solve the iframe issue. just make sure the pdf is publicly accessible!

I encountered a similar issue when trying to embed Google Docs PDFs. The solution that worked for me was using the Google Drive File ID instead of the direct link. Here’s how you can modify your approach:

After uploading, extract the file ID from the document URL and use this format for the iframe src:

https://drive.google.com/file/d/{FILE_ID}/preview

For example:

def get_pdf_file_id(doc):
    # Extract file ID from the alternate link
    return doc.get_alternate_link().href.split('/')[-2]

file_id = get_pdf_file_id(doc)
iframe_html = f'<iframe src="https://drive.google.com/file/d/{file_id}/preview"></iframe>'

This method should work seamlessly within an iframe without redirecting issues. Remember to set appropriate sharing permissions for the document in Google Drive.