Display .docx (.doc) on Browser without downloading in php

leonardeveloper picture leonardeveloper · Jan 5, 2014 · Viewed 30.2k times · Source

Is there any alternative way of viewing a file(locally/online) in PHP? Since I want to display a .docx (.doc) file in my browser but it keeps on downloading it. So anyone who has a code for that? I keep on searching some other source code but no luck. I tried using Iframe but still keeps on downloading the file. Thanks!

Answer

Aamir picture Aamir · Jan 29, 2016

An easy solution to display almost every type of document in browser using iframe.

Solution is Google Docs Viewer

Just use iframe as given below and replace url of document in it.

<iframe src='https://docs.google.com/viewer?url=ENTER URL OF YOUR DOCUMENT HERE&embedded=true' frameborder='0'></iframe>

By Replacing sample url for .docx file, above code becomes.

<iframe src='https://docs.google.com/viewer?url=http://calibre-ebook.com/downloads/demos/demo.docx&embedded=true' frameborder='0'></iframe>

By Replacing sample url for .pdf file, above code becomes.

<iframe src='https://docs.google.com/viewer?url=http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf&embedded=true' frameborder='0'></iframe>

Note: Document that you need to display in browser must have correct url and must be publicly available, because Google needs to fetch and display document, process fails if document is private and you will see iframe with NO PREVIEW AVAILABLE