Batch Document Conversions with Libreoffice
A few days ago, I was having trouble with some powerpoint documents I was given. I needed to read them on my netbook while traveling. The problem was that my netbook is a magnificent Asus eeePC 701 which, of course, isn’t powerful enough to run libreoffice. So I had to convert them, but with what ? I didn’t want to use Libreoffice to open each documents and export them to pdf : there was a dozen of them ! And then, I discovered libreoffice headless mode.
Headless Mode
Documented almost nowhere and hidden inside the libreoffice man, there is this option : the headless (Without GUI) mode. So how do you use libreoffice headless mode ? It’s pretty simple. Open and terminal and type :
libreoffice --headless anyParameterYouWant
File conversion ?
libreoffice --headless --convert-to DestinationFormat[:OutputFilterName] File…
So if you want to convert all your .ppt files in your lol folder into nice .pdf files, you just have to type :
libreoffice --headless --convert-to pdf lol/*.ppt
Filter list
So, what is this OutputFilterName ? Well it’s apparently the name of the dialog used for export. Normally, you shouldn’t need it but just in case here is all the informations that I could find on it :
Libreoffice’s documentation being what it is, I was unable to find anything in it. However, I found a list from Open Office 3.0’s (So, before the fork) documentation.
May the Force be with you.
File formats
What can you convert to what ? Basically, you can import anything that libreoffice an import and export it to anything libreoffice can export.
Once again because of the magnificense of libreoffice’s documentation, I invite you to read the list I previously gave.
May the Force be with you.
Runing two libreoffice instance
In order for your batch conversion to work, you need not to have a libreoffice instance already running. That’s kind of bothersome, you know. What if you want to work on your thesis (although, you must be crazy to want to write a thesis using Libreoffice ( or Open Office, or Microsoft Office), you should use serious tools like LaTeX) and convert a batch of documents at the same time ? Well, if you don’t know this trick, you are doomed.
In order to acomplish this, once again, poorly documented trick, you need to use a libreoffice environment variable to make libreoffice use another profile. You need to add the following command parameter into the mix
-env:UserInstallation="file://path/where/you/want/your/profile/to/be"
For example, this will create a hidden .libreoffice-alt folder in your home :
-env:UserInstallation="file://$HOME/.libreoffice-alt"
And then, the whole command :
libreoffice --headless -env:UserInstallation="file://$HOME/.libreoffice-alt" --convert-to pdf lol/*.ppt
Ok, I think it is time to make this easier to manage.
Libreoffice as a serice
Now is the time when you start thinking I am mad. Maybe I am, however, if you continue reading a little while, you will understand why it is not such a terrible idea.
Libreoffice offers an API that allows to use UNO to connect and do certain tasks such as file conversions. So, to launch a listening instance of libreoffice you need to add this parameter :
--accept="socket,host=127.0.0.1,port=PORT;urp;StarOffice.Service"
Continuing the example :
libreoffice --headless -env:UserInstallation="file://$HOME/.libreoffice-alt" --accept="socket,host=127.0.0.1,port=8100;urp;StarOffice.Service --convert-to pdf lol/*.ppt
Notice the StarOffice.Service. I have absolutely no idea why.
Systemd for the win
So, let’s make this into a proper service using, of course, systemd.
This is an dirty but working systemd unit file that is licensed under the WTFPL
You can place the unitfile into /etc/systemd/user/
or ~/.config/systemd/user/
. For the sake of my example, I will name it libreoffice.service
[Unit]
Description=Libreoffice UNO acceptor
[Service]
Type=simple
ExecStart=/usr/bin/libreoffice --headless -env:UserInstallation="file://$HOME/.libreoffice-alt" --accept="socket,host=127.0.0.1,port=8\
100;urp;StarOffice.Service"
[Install]
WantedBy=default.target
Unoconv
Now, you need to connect to this UNO acceptor. I recommend you a program called Unoconv. Originally created for Open Office, it works perfectly fine with Libre Office. Normally, your UNO acceptor should be automatically recognized, if not use the -c "connectionString"
parameter.
In order to convert a document you need to use the following syntax :
unoconv -f FORMAT file…
Easier than that, don’t you think ?
libreoffice --headless -env:UserInstallation="file://$HOME/.libreoffice-alt" --convert-to pdf lol/*.ppt
tl;dr
Libreoffice can convert documents using cli. You need to specify a custom environment to do so while using libreoffice to read or edit documents. You can even run a service that way, of course, with systemd too. Oh and Unoconv is a software with a clear syntax but it cannot run if you have another libreoffice instance so you need a service with a custom environment.
Oh and if you need to write serious documents, do yourself a favor and use LaTeX.
Bibliography
I had to do a lot of researches to write this article so here are the pages that were pertinent.