Printing ODT documents from the command line (and mass PDF exporting)

I recently had to print a collection of documents I had created over the year and I really was not thrilled at the prospect of having to open the file, Command+P, Enter, Command+W, Command+Tab, open new document, rinse, repeat for some 60 odd documents. All the documents in question were ODT documents created using either OpenOffice or LibreOffice, depending on how old they were.

A bit of research on command-line printing using OpenOffice and LibreOffice led me to some outdated posts that didn't quite give me the information I was looking for, but gave me a good place to start: the Writer application's binary is named 'soffice' and accepts various command-line arguments. A quick search revealed that the file does indeed exist inside the OS X application file:

$ find /Applications/LibreOffice.app -name soffice
/Applications/LibreOffice.app/Contents/MacOS/soffice

(for those that don't know, OS X applications are actually just plain folders that hold the application's metadata, executables and related files)

What's even better is that LibreOffice now has an option to do exactly what I was looking for:

$ /Applications/LibreOffice.app/Contents/MacOS/soffice -h
LibreOffice 3.5

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

Options:
--minimized    keep startup bitmap minimized.
[...]
--print-to-file [-printer-name printer_name] [--outdir output_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

Using the "--print-to-file" parameter will convert any document that LibreOffice can read into a raw postscript file by the same name.

Although I could easily send the resulting PostScript files to the printer using CUPS' lp utility, I wanted to take this one step further. Each of these documents contained the same colour logo and I wanted to avoid wasting any ink. Another search brought me to this question on SuperUser.com, where ysis's answer demonstrated that the gs (GhostScript) utility can do this and even merge all the documents into one file:

gs -sOutputFile=converted.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibiltyLevel=1.4 -dNOPAUSE -dBATCH input1.ps input2.ps input3.ps

After about 30 seconds of processing all of the 60 files were there, in blank and white, concatenated into converted.pdf. Printed it using OS X's Preview application using the "fast draft" setting and I had all my 60 documents printed with minimal ink used in less than 5 minutes!

Comments

I'm trying to follow your instructions, but my commands aren't working. Could you paste the literal command that you used successfully? e.g. this command doesn't work for me: libreoffice --print-to-file * --outdir ps/

Hi Sam,

I'm not sure what platform you're on, but it may be necessary to replace libreoffice in the command with LibreOffice's specific binary corresponding to LibreOffice Writer (on OS X, that's soffice).

I also remember experimenting with its other batch options (possibly --outdir) and remember having problems. In my case it was simplest to simply use something like this:

/Applications/LibreOffice.app/Contents/MacOS/soffice --print-to-file container-folder/*.odt

and then grab mass move the .ps files to another folder:

mkdir new_ps_files
find . -name '*.ps' | while read line;do
  mv "$line" new_ps_files
done