LibreOffice Headless Operation

Post image for LibreOffice Headless Operation

by Rex Djere on April 1, 2012

in Training


1. Description
Headless is a package that allows LibreOffice to function in a command-line mode without an X server. By running the command-line command “libreoffice” with the “–headless” flag, LibreOffice can perform tasks such as converting individual or batch files from one format to another.

2. Example
To run LibreOffice in headless mode, all graphical instances of LibreOffice must first be closed. The Linux command below would convert the Open Document text file Files.odt in user bob’s working directory to a pdf file named Files.pdf. The command was successfully tested in a Fedora 16 terminal.

[bob@fedoralaptop ~]$ /usr/bin/libreoffice –headless –invisible –convert-to pdf Files.odt

Figure Headless.1: LibreOffice Headless Conversion of an Odt File to a Pdf File

Running the command above creates a file named “Files.pdf” in the same directory as “Files.odt”.

3. Learning More About Headless Operation Via the Manpage
In Headless mode, LibreOffice can convert between multiple file formats. For example, if you wanted to convert Files.odt to html, you would change “–convert-to pdf” to “convert-to html”. If you get stuck at anytime while doing file operations, you can view the libreoffice manpage by issuing this command:

[yourusername@yourdevice ~]$ /usr/bin/libreoffice –help

Figure Headless.2: Getting Help in LibreOffice

For example, running the command above gave me all of the options available to me in headless mode. The output of my terminal window when I ran the command is below. Note that instead of starting the command with libreoffice, you can use soffice instead.

Figure Headless.3: LibreOffice Manpage Is Below

Usage: soffice [options] [documents...]

–minimized keep startup bitmap minimized.
–invisible no startup screen, no default document and no UI.
–norestore suppress restart/restore after fatal errors.
–quickstart starts the quickstart service
–nologo don’t show startup screen.
–nolockcheck don’t check for remote instances using the installation
–nodefault don’t start with an empty document
–headless like invisible but no userinteraction at all.
–help/-h/-? show this message and exit.
–version display the version information.
–writer create new text document.
–calc create new spreadsheet document.
–draw create new drawing.
–impress create new presentation.
–base create new database.
–math create new formula.
–global create new global document.
–web create new HTML document.
-o open documents regardless whether they are templates or not.
-n always open documents as new files (use as template).

Specify X-Display to use in Unix/X11 versions.
print the specified documents on the default printer.
print the specified documents on the specified printer.
open the specified documents in viewer-(readonly-)mode.
–show open the specified presentation and start it immediately

Specify an UNO connect-string to create an UNO acceptor through which
other programs can connect to access the API.

Close an acceptor that was created with -accept=
Use -unnaccept=all to close all open acceptors
Force an input filter type if possible
Eg. -infilter=”Calc Office Open XML”
–convert-to output_file_extension[:output_filter_name] [-outdir ouput_dir] files
Batch convert files.
If -outdir is not specified then current working dir is used as output_dir.
Eg. -convert-to pdf *.doc
-convert-to pdf:writer_pdf_Export -outdir /home/user *.doc
–print-to-file [-printer-name printer_name] [-outdir ouput_dir] files
Batch print files to file.
If -outdir is not specified then current working dir is used as output_dir.
Eg. -print-to-file *.doc
-print-to-file -printer-name nasty_lowres_printer -outdir /home/user *.doc

Remaining arguments will be treated as filenames
or URLs of documents to open.

Previous post:

Next post: