How to convert libreoffice ODT to PDF in bash

One possible approach is to install unoconv (if not already installed) and

unoconv file.odt

Please see man unoconv for details

unoconv is a command line utility that can convert any file format that LibreOffice can import, to any file format that LibreOffice is capable of exporting. unoconv uses the LibreOffice’s UNO bindings for non-interactive conversion....

In some platforms it is also necessary to install libreoffice-headless \thanks{Aaron}


That is unlikely going to work, as the suggestion in the comment is both incomplete (you cannot just specify some directory) and incorrect (--env:... should be -env:... Here is what I recommend you do:

  1. Stop all instances of libreoffice
  2. Start libreoffice from the commandline without specifying --headless:

    libreoffice -env:UserInstallation=file:///home/username/.config/libreoffice-alt
    

    you should replace /home/username with your home directory (and adjust .config if you don't have that on your CentOS, I did this on Ubuntu and Linux Mint). The above will create a new configuration directory for the alternate libreoffice in your .config directory, without which you would get some error about java not being found.

  3. Exit that instance of libreoffice
  4. That directory /home/username/.config/libreoffice-alt should now have been created for you.

Now start another libreoffice from the command-line (doing so allows you to see some useful messages if things go wrong when starting the second instance), without the -env:..., and while that is still running start the conversion using:

libreoffice -env:UserInstallation=file:///home/username/.config/libreoffice-alt --headless --convert-to pdf *.odt

Here is a totally different approach.

It is possible, because in recent times a series of new conversion paths were opened by Pandoc's newly acquired capability to read ODT files.

When Pandoc reads in a file format, it converts it into an internal format, "native" (which is a form of JSON).

From its native form, it can then export the document into a whole range of other formats. Not only PDF, but also DocBook, HTML, EPUB, DOCX, ASCIIdoc, DokuWiki, MediaWiki and what-not...

Since here the wanted output format is PDF, we have another choice of different paths, provided by what Pandoc is calling a pdf-engine. Here is the list of currently available PDF engines (valid for Pandoc v2.7.2 and later -- previous versions may support only a smaller list):

  • pdflatex: This requires LaTeX to be installed in addition to Pandoc.

  • xelatex: This requires XeLaTeX to be installed in addition to Pandoc (also available as an additional package to general TeX distributions).

  • context: This requires ConTeXt to be installed in addition to Pandoc; ConTeXt is available as an additional package to most general TeX distributions).

  • lualatex: This requires LuaTeX to be installed in addition to Pandoc (also available as an additional package to general TeX distributions).

  • pdfroff: This requires GNU Roff to be installed in addition to Pandoc.

  • wkhtml2pdf: This requires wkhtmltopdf to be installed in addition to Pandoc.

  • prince: This requires PrinceXML to be installed in addition to Pandoc.

  • weasyprint: This requires weasyprint to be installed in addition to Pandoc.

There are some more and newer PDF engines now integrated into Pandoc, which I have not yet used myself and which I currently cannot describe in more detail: tectonic and latexmk.

WARNING: Do not expect that the appearance of your original document will be identical in all the PDF outputs to the print preview or PDF export of the ODT! Pandoc, when converting does not preserve layouts, it preserves the contents and the structure of documents: paragraphs remain paragraphs, emphasized words remain emphasized, headings remain headings, etc. But the overall look can change considerably.

Example commands

pdflatex:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=pdflatex

XeLaTeX:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=xelatex

LuaLaTeX:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=lualatex

ConTeXt:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=context

GNU troff:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=pdfroff

wkhtmltopdf:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=wkhtml2pdf

PrinceXML:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=prince

weasyprint:

 pandoc -f odt -o mydoc.pdf mydoc.odt --pdf-engine=weasyprint

Above commands are the most basic for the conversion. Depending on the PDF engine you pick, there may be many other options possible to control the appearance of the output PDF file. For example, the following additional parameters may be added to all those paths routing through LaTeX:

 -V geometry:"paperwidth=23.3cm, paperheight=1000pt, margin=11.2mm, top=2cm"

which will use a custom page size (a bit larger than DIN A4) with margins of 2cm on the top edge and 1.12cm at the other three edges).