June 22, 2017 Webby

Usage from JVM scripting languages

Possibly one of the main usages of JODConverter is to perform batch conversions from the command line. For this reason JODConverter includes a simple command line tool.

However sometimes you need to customise the conversion settings, for example PDF output quality or encryption, while the provided command line only works with predefined conversions. Or you want to recursively search for input documents and output converted files to a different directory. In any case you don’t really want to write a Java class for such a task.

This is a perfect scenario for using a scripting language. There are a number of scripting languages for the JVM these days: Groovy, JRuby, Jython, Beanshell, etc. Using an interpreted language also means you can easily modify the script in a simple text editor and execute it without recompilation.

Here is an example using Groovy 1.5. It converts word processor documents to PDF limiting image resolution to 300 DPI (to reduce PDF size).

import com.artofsolving.jodconverter.*
import com.artofsolving.jodconverter.openoffice.connection.*
import com.artofsolving.jodconverter.openoffice.converter.*

if (args.length < 2) {
    println "USAGE: ConvertToPdf.groovy <input-file> <output-file>"
    System.exit(255)
}
def input = new File(args[0])
def output = new File(args[1])

def connection = new SocketOpenOfficeConnection(8100)
connection.connect()

def registry = new DefaultDocumentFormatRegistry()
def converter = new OpenOfficeDocumentConverter(connection, registry)

def pdf = registry.getFormatByFileExtension("pdf")
def pdfOptions = [ 'ReduceImageResolution': true, 'MaxImageResolution': 300 ]
pdf.setExportOption(DocumentFamily.TEXT, "FilterData", pdfOptions)

converter.convert(input, output, pdf)
connection.disconnect()

To run this example, you need to

  1. Download Groovy 1.5 and unpack it
  2. Copy all the JAR files in jodconverter-2.2.1/lib to groovy-1.5.0/lib
  3. Copy the above code to a file, say ConvertToPdf.groovy
  4. Execute groovy-1.5.0/bin/groovy ConvertToPdf.groovy input.doc output.pdf