How to convert a .docx .doc MS Word file to pdf in command line on Linux

How to convert a MS Word document file such as .docx and .doc to pdf on Linux using command line tools?

Use LibreOffice:

libreoffice --headless --convert-to pdf 
--outdir /path/to/out/dir/ /path/to/doc/docx/file
Answered by Eric Z Ma.

While this LibreOffice answer works, the –headless option still needs as display, even though it does not use it. In otherwords, this option won’t work on a truely headless server or from a cron or batch job, unless you fire up a dummy X server (eg xvfb). The LibreOffice people don’t seem inclined to “fix” this:

Hi Robert, thanks for the notes!

So in a server without a real X, it seems a command like this is needed (not yet tested):

xvfb-run libreoffice --headless --convert-to pdf 
--outdir /path/to/out/dir/ /path/to/doc/docx/file

to make it run by cron or in background.

TextMaker, the word processor included in SoftMaker FreeOffice, has a much better compatibility with Microsoft Word compared to LibreOffice. You should download FreeOffice without charge, open your doc/docx documents faithfully with TextMaker, press the pdf button, and get a perfect pdf file.

Does FreeOffice provides a command-line method to generate PDF from word .doc or .docx files? That is required to automatically generating many files.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *