Got a Pile of Word Docs? Turn Them into PDFs on Linux

Skip the clickfest. Convert Word docs to PDFs from the Linux terminal and sort out the formatting snags that trip people up. Got a pile of Word docs? Turn them into PDFs on Linux

Opening Word documents one by one just to save them as PDFs gets old fast. LibreOffice can do the job from the Linux terminal, whether you've got one file or a folder full of them.

The command is simple. The formatting can be a little less predictable, especially when the document uses fonts your computer doesn't have. Here's how to convert the files and what to check when a PDF comes out looking wonky.

Make your first PDF

First, check that LibreOffice Writer is installed:

libreoffice --version

From the folder with your Word document, run:

mkdir -p pdf-output

libreoffice --headless --convert-to pdf \
 --outdir pdf-output "Quarterly report.docx"

You'll find the result at pdf-output/Quarterly report.pdf.

--headless runs LibreOffice without opening its usual window. --convert-to pdf picks PDF as the output format, and --outdir tells it where to put the result. Without --outdir, the PDF goes into your current folder. LibreOffice's command-line guide has the full list of options.

Keep the quotation marks if your filename has spaces. Starting with a new output folder also helps you avoid confusing today's PDFs with older ones.

Convert a folder of DOCX files

To convert every lowercase .docx file in the current folder:

mkdir -p pdf-output

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

This only checks the folder you're in. It won't search through subfolders, and on a typical Linux system *.docx won't catch files ending in .DOCX.

Make sure there are matching files before you run the command. If your documents live in several folders and share names such as report.docx, give each folder its own output directory. Otherwise, PDFs with the same name can clash.

When the batch is done, open a few PDFs. Check the first and last pages, plus any documents with tables, images, or tricky layouts. A conversion finishing doesn't mean every page looks right.

Export only the pages you need

Need just pages two through five? Add a page range to the PDF export option:

libreoffice --headless \
 --convert-to 'pdf:writer_pdf_Export:{"PageRange":{"type":"string","value":"2-5"}}' \
 --outdir pdf-output "Quarterly report.docx"

The single quotes keep Bash from interpreting the settings inside. LibreOffice lists page ranges and other options in its PDF export guide.

Give the PDF a quick look before sending it. If LibreOffice lays out the document differently from Word, the page numbers might not match what you expected.

Why does my PDF look different?

A PDF can convert without errors and still have shifted page breaks, squashed tables, or odd spacing. LibreOffice and Microsoft Word don't handle every complex document feature in exactly the same way. LibreOffice calls out possible differences with tables, frames, revision marks, and other formatting in its Microsoft Office compatibility notes.

Fonts are a common culprit. If your document uses a font that isn't installed on Linux, LibreOffice can substitute another one. The replacement may take up more or less space, which can move text onto another page. LibreOffice's font settings let you manage font replacements.

Try this:

  • If you control the document, use fonts available on the computer that will make the PDF.
  • If someone sent you the DOCX, install the original font if its licence allows it.
  • Open the file in Writer and check the pages where the layout changed.
  • If the exact Word layout matters, ask the author to export the final PDF from Word.

Changing the command won't fix every layout difference. Sometimes the document needs a small formatting adjustment first.

No PDF? Check these things

What happenedWhat to check
The terminal says libreoffice: command not foundLibreOffice may not be installed, or its command may not be available in your shell.
It can't find the DOCXCheck the spelling, quotes, and folder you're running the command from.
The PDF isn't where you expectedCheck the --outdir path. Without it, LibreOffice uses your current folder.
It works by hand but fails in a scheduled jobThe job may use a different account or lack access to the files. Try absolute paths.
One document gets stuckSee if it opens in Writer. It may need a password or other interaction.

LibreOffice needs permission to write to its user profile. If a scheduled job or another LibreOffice window gets in the way, try giving this conversion its own profile:

libreoffice \
 -env:UserInstallation=file:///home/alex/.cache/libreoffice-convert \
 --headless --convert-to pdf \
 --outdir pdf-output "Quarterly report.docx"

Change /home/alex/.cache/libreoffice-convert to a writable folder on your machine. The separate profile keeps this conversion's settings away from your usual LibreOffice setup. LibreOffice's startup guide explains the profile option.

For everyday letters and reports, the basic command is often enough. For a big batch or a fussy layout, check the fonts and look over a few PDFs before calling it done.

No comment yet

Be the first to share your thoughts.