11

I am using libreoffice under CentOS 6. I can convert ODT files to PDF with:

libreoffice --headless --convert-to pdf *.odt but the problem is that it only works when no document is open in libreoffice.

When I specify --env:UserInstallation=file:///path/to/some/directory as suggested in one of the comments of this question , it doesn't help.

What am I doing wrong? It is a nuisance to close all libreoffice instances before running the before command.

8

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
3
  • PS I have .config as well on CentOS – Johan Apr 18 '15 at 15:22
  • Instead of stopping all instances, you can simply log in as a different user, create the .config-files and copy those to your own home dir. – Ole Tange Feb 26 '18 at 20:21
  • 1
    On an Ubuntu 16.04 machine, I just ran libreoffice --headless --convert-to pdf <filename>.odt and it did it, without those prep steps. It did print a warning about Java tho. – Kyle Aug 31 '18 at 23:10
8

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}

4
  • Under CentOS7, I also had to add the package libreoffice-headless to have it automated. If the user has never used it before, the first call to unoconv fails with error 81 after creating a bunch of files in ~/.config/. – Aaron D. Marasco Feb 11 '16 at 14:32
  • 1
    @AaronD.Marasco, thank you very much for this important information! Probably there is a dependency missing in the spec file. I will add your tip to the answer. – JJoao Feb 12 '16 at 14:53
  • 1
    @JJoao, if you are a CentOS user, you should report your finding as a bug report. – vonbrand Feb 12 '16 at 19:32
  • Not a spec dependency... just a use case. The error 81 issue is supposedly fixed but I don't know what version; I guess CentOS doesn't have it yet. – Aaron D. Marasco Feb 12 '16 at 19:34
3

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).

4
  • Unfortunately pandoc doesn't support every libreoffice format; for example .ods (the spreadsheet format) is not supported. – Granitosaurus Nov 11 '19 at 2:59
  • @Granitosaurus: The question was about ODT, hence this answer is appropriate. – Kurt Pfeifle Nov 11 '19 at 23:53
  • Oh I never implied it wasn't appropriate, just that it's unfortunate pandoc doesn't support many other libreoffice formats. – Granitosaurus Nov 14 '19 at 5:47
  • @Granitosaurus: Pandoc is an OpenSource Software project. So you can easily contribute. If you're not into programming additional features into Pandoc, you at least can find their project on Github and are free to submit a Feature Request into their bug tracker. That also was what triggered Pandoc support for PowerPoint output to be added... – Kurt Pfeifle Nov 14 '19 at 12:37
0

You can try this :

$ libreoffice2 pdf *.odt

Anyway, this pb. has been fixed the libreoffice Team since at least libreoffice 6.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.