File preview seems to be a common thing, so many developers might think that asking Google How to preview Word document in browser? would be enough to find a perfect solution for their project, because how such a popular thing like file preview can be so hard to achieve?
Looking it up on Google will most likely lead you to use Google docs viewer or inline HTML file embedding eg. with no specific information about the problems that both of these solutions have.
What do I mean by file preview? I mean viewing a file on a website, the same way as on a desktop, within a dedicated app. If you want to preview your file, all you need is direct the URL to the file, but how can you know that you have a direct URL? For most cases, pasting it in your browser will automatically force a download process (except pdf’s, most likely they’ll open up in your browser window). Let’s see how this can be implemented.
IN THIS BLOG POST and 2 more chaptersInline embedders are: , and , their way of working is pretty much the same, they’ve got slight differences which are not significant for us at this moment.
Add inline embedder element on your website and in src attribute pass it a direct URL to your file.
Pros:
Cons:
In this section, I’ll give some information about free online doc previews, unfortunately, there are only two and none of them is perfect, but they greatly expand the possibilities. So here we have our money savers: Google Docs and Office Web Apps.
Just append your src attribute with an appropriate URL to a specific doc viewer, it will download your file from URL and then generate an HTML page from it, and then you direct your iframe to it and voila! Of course… there are some limitations to this but not as bad as you might think.
This is not an official solution, I mean Google nowhere gives you documentation on how to properly use this, but developers somehow found it out anyway, despite that Google Docs Viewer isn’t supported anymore it still works!
Pros:
Cons:
Microsoft also gives its solution to preview files on your website, surely it’s the best option for Office types files because it’s the best at parsing them into HTML.
Props:
Cons:
Now we got all the information, now we need to make the final decision, let’s get into it.
Let’s deal with Google Docs random errors Firstly, I wanted to say that I know it’s not a very elegant solution but I believe it’s the only way to do it.
The important thing here is that HTML inline content embedders eg. does not allow you to interfere with its content when it’s completely loaded, so… In JavaScript create an Interval which every few seconds will try to get into your iframe content, if it has thrown an error, that means the content is loaded properly and you can stop the interval, if no error has been thrown, you need to refresh your iframe by setting it’s src attribute again then repeat everything again until it’s properly loaded.
Other solutions to embed your files on your site If you would like to check out some ready to use packages that somehow relate to file embedding, here are some links that I found:
Keep in mind, I haven’t used any of them, so I can’t recommend any in particular.
Tools for previewing files in a browser will differ depending on the type of file and device you’re using. Each one has its own pros and cons, so it’s generally up to personal preference which one you choose.
I am a React Tech Leader with a holistic approach to crafting code. Thanks to my years of experience I build clean and maintainable software solutions for our clients. I always take ownership of the application and put extra effort to improve the UX and assumptions behind the features I implement.