DEV Community

loading...

Rails : How to preview Office files ?

pobb profile image pob ・2 min read

We use to work with CSV or PDF files when we talk about web apps. CSV are easy to use and edit after exportation (we can also easily create them), PDF are easy to read and and pretty much a standard. But when we have to deal with .docx, .xslx or similar formats, things get complicated.

Preview a file

In your app, you have certainly used a preview for images or PDFs files. But how will you do in case of an emails app that must show preview of all attachments or another DMS app that must preview files with exotic extensions (docx, pptx, xslx, odt, ods, odp etc.) ?

Convert them to PDF and you are done !

Libre office to the rescue

To get this tool, just install libreoffice on your server using :

apt-get install libreoffice -qq

A little look to the doc and you can see two options we will use :

--convert-to : allow us to convert a file from our extension to pdf
--headless : it ignore GUI environment

libreoffice --headless --convert-to pdf my_file.docx

Rails : there is a gem for that

In a rails app, just add a gem on your Gemfile :

# Gemfile
gem 'libreconv'

# In your ruby file module or class
Libreconv.convert("#{Rails.root}/public/files/my_file.docx", "#{Rails.root}/public/files/my_file.pdf")

Bundle install, and now you can make a preview of your file using the PDF version on your server. Note that libreoffice's tool don't convert only in PDF but can convert a file from extension that it can open and read to a file with extension that he can write. PDF won't disappoint you ;)

Discussion (7)

Collapse
robbikmansurov profile image
RobBikmansurov

Hi, Pobb! It`s interesting for me.
Did you try to use the unoconv gem? Did you compare this tools?
Which is better to use?

Collapse
pobb profile image
pob Author

Hello. I didn't use unoconv but it seem to do good job too ! I choose my tools if they are standarized. For example, on a server "libreoffice" is on official package. So I can use it easily. Next, I found that the libreconv gem got lot of download on rubygems, and I found the "libreoffice" command that I used in CLI in the gem source code. But in general purpose, unoconv looks really good too :)

Collapse
robbikmansurov profile image
Collapse
leduccuong1612 profile image
Lê Đức Cường • Edited

Hi, Pod, Can you help me.
When i try to use libre gem, i get this error, but i already install Libre office on my laptop, and i using win10, does it matter? ... sorry for my bad english

IOError at /users/8/request
Can't find LibreOffice or OpenOffice executable.

Collapse
pobb profile image
pob Author

Hello, I can tell you that on Linux and Mac it works well but sorry I don't develop on Windows. I highly suggest you to create a linux virtual machine or use docker to play with ruby :) Sorry I don't get solutions for windows.

Collapse
archonic profile image
Archonic

I would imagine most people looking to do this in 2020 and beyond will be looking for a way to do it through ActiveStorage.

Collapse
pobb profile image
pob Author

You are totally right and it can be done using ActiveStorage (ActiveStorage::Previewer that use libreoffice as well). My context was a little bit different it was an old ruby api, no ActiveStorage context available. I should have mention it in the title.

Forem Open with the Forem app