Inconsistent loading of Google Docs viewer in iframe

Google Docs viewer acting up in my iframe

I’m having a weird issue with embedding the Google Docs viewer in my webpage. The iframe I’m using to load the viewer sometimes displays the document correctly, but at other times, it just shows a blank area.

I’ve tried different PDF URLs and even opened the viewer link directly in my browser, but the inconsistency remains. Does anyone know a way to make this embedding work reliably or suggest an alternative method to display PDFs?

<iframe src="https://docs.google.com/viewer?url=SOME_PDF_LINK&embedded=true"></iframe>

I’ve run into this exact issue before, and it can be quite frustrating. In my experience, the inconsistency often stems from how Google’s servers handle the request. One workaround I found effective was to add a random query parameter to the iframe src URL. This forces a fresh request each time:

<iframe src="https://docs.google.com/viewer?url=SOME_PDF_LINK&embedded=true&rand=" + Math.random()></iframe>

Another approach that worked for me was switching to PDF.js, an open-source PDF viewer. It’s more reliable and gives you greater control over the display. You’ll need to host the library yourself, but it’s worth it for the consistency.

If you’re set on using Google Docs viewer, you might also try implementing a reload mechanism if the iframe fails to load properly. It’s not ideal, but it can help mitigate the issue in some cases.

hey, i’ve dealt with this too. try using mozilla’s PDF.js instead. it’s way more reliable and you can customize it. plus, you don’t have to rely on google’s servers. here’s a quick example:

<script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
<canvas id="pdf-render"></canvas>

then use javascript to load and render the PDF. Works like a charm!