Learn how to convert a word file to PDF in libreoffice with the command line interface.

How to convert a Word file to PDF (docx to pdf) in LibreOffice with the CLI in Ubuntu 20.04

In some applications, it will be necessary to automatize the conversion of documents between different formats. The most popular ones and the ones you will probably end up using are Word documents and PDFs. In this tutorial, we'll show you how to convert an Office Word document to PDF with the LibreOffice CLI in Ubuntu 20.04, in both desktop and server versions.

1. Install headless LibreOffice

LibreOffice is a powerful and free office suite, a successor to OpenOffice(.org), used by millions of people around the world. Its clean interface and feature-rich tools help you unleash your creativity and enhance your productivity. LibreOffice includes several applications that make it the most versatile Free and Open Source office suite on the market: Writer (word processing), Calc (spreadsheets), Impress (presentations), Draw (vector graphics and flowcharts), Base (databases), and Math (formula editing).

This awesome applications offers a headless version that can be used from a terminal on your server or desktop. To use this in Ubuntu, be sure to update the repositories before continuing:

sudo apt-get update

And proceed with the installation of LibreOffice with the following command:

sudo apt-get install libreoffice --no-install-recommends

2. Install Java Runtime Environment (JRE)

As next, be sure to have the Java Runtime Environment installed in your server using java -version. If the command fails, it means that you don't have Java installed, so you need to install it through APT with the following command:

sudo apt-get install default-jre

If you don't install Java, you may find the following warning when trying to convert your document (the conversion will work though):

javaldx: Could not find a Java Runtime Environment!
Please ensure that a JVM and the package libreoffice-java-common
is installed.
If it is already installed then try removing ~/.config/libreoffice/4/user/config/javasettings_Linux_*.xml
Warning: failed to read path from javaldx
convert /home/ourcodeworld/Desktop/mydocument.docx -> /home/ourcodeworld/Desktop/mydocument.pdf using filter : writer_pdf_Export

The installation of Java will take about 180MB of space in your disk.

3. Convertir DOCX to PDF

Finally, after installing LibreOffice you will be able to use it to convert your Microsoft Office Word document to PDF with the following command, whose parameters are described like this:

libreoffice --headless --convert-to pdf <input-word-file.docx> --outdir </path/to/store/files>
  • --headless: this argument launches libreoffice in headless mode, without UI.
  • --convert-to <format>: this argument specifies that the tool will be used to convert the input file to a new format, which is given as positional argument of this parameter, in our case PDF.
  • --outdir: the directory where the converted file should be stored. In our case ./ indicates that the file should be saved in the current directory.

For example with a real Office 2013 document:

libreoffice --headless --convert-to pdf MyWordFile.docx --outdir ./

The only problem that exists till the date, is the fact that you cannot change the name of the output file. For example, if the input file is MyWordFile.docx, the output file will be MyWordFile.pdf, and it seems there's no way to change this, so you will need to stick to this nomenclature.

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors