How to Run LibreOffice Headless via Command Line

Running LibreOffice in headless mode allows you to execute office suite operations—such as converting documents, batch processing files, and printing—directly from the terminal or automated scripts without launching the graphical user interface (GUI). This guide explains the core command syntax, essential flags, and practical examples for running LibreOffice headless across different operating systems.

Basic Syntax and Key Flags

The general syntax for executing LibreOffice commands in headless mode is:

libreoffice --headless [options] [file]

(Note: Depending on your operating system or installation, the binary name may be soffice instead of libreoffice.)

Key flags include: * --headless: Runs LibreOffice without opening the graphical interface or user windows. * --convert-to <output_extension>: Converts the input document into the specified file format. * --outdir <directory_path>: Defines the target folder where the converted output will be saved. * --print-to-file: Prints the file to an output format without UI interaction.


Common Use Cases

1. Converting a Document to PDF

To convert a single document (such as a .docx, .odt, or .xlsx file) directly into a PDF:

libreoffice --headless --convert-to pdf document.docx

2. Specifying an Output Directory

By default, converted files are placed in the current working directory. Use --outdir to specify a different destination:

libreoffice --headless --convert-to pdf document.docx --outdir /path/to/output/

3. Batch Converting Files

You can convert multiple files at once using shell wildcards:

libreoffice --headless --convert-to pdf *.docx --outdir /path/to/output/

4. Converting Between Other Formats

Headless mode supports conversions between various supported formats, such as HTML, plain text, or image formats:


Operating System Path Details

If the libreoffice or soffice command is not recognized in your system’s PATH, use the full binary path: