Converting files from one format to another is one of those problems that sound simple but actually is very complex to solve. In one of my projects, I needed to convert spreadsheets to comma separated files and I hit upon JODConverter, the Java OpenDocument Converter. This offers a variety of usage options – it can be used as a Java library, as a web service etc. The learning in using JODConverter is good and huge !!
As the name indicates, JODConverter requires Java and OpenOffice. It basically automates the conversions that are possible using OpenOffice. And for this, it requires OpenOffice to run as a service and herein lies what I feel is the biggest drawback of the tool. I just could not get the OO service to run on a Linux box whereas the Windows experience was smooth and straightforward !!
Creating the OpenOffice service in Windows:
To create a service in Windows, follow the steps provided here. The service should be named as OpenOfficeUnoServer with the Application value set as PATH_TO_OPENOFFICE/program/soffice.exe. We need to add another value to the Parameters key named AppParameters and set it to -headless -accept=socket,host=0.0.0.0,port=8100;urp;
When the service is started, it runs as a special account and OpenOffice will try to show the license agreement dialog as if it was a new user instead of just starting the application. To avoid this, we need to edit an XML file to let OpenOffice think that we have already opened it at least once.
In cases of OpenOffice 2.x, the file to be edited is PATH_TO_OPENOFFICE/share/registry/data/org/openoffice/Setup.xcu and in OpenOffice 3, the file is PATH_TO_OPENOFFICE/Basis/share/registry/data/org/openoffice/Setup.xcu
When OpenOffice is uninstalled or upgraded, the existing service will cease to run and the whole process needs to be repeated to create the service again.
Usage as a Java Library:
Perhaps the simplest of the available options ! Just write a Java snippet to connect to an instance of OO listening on a port (8100, by default) and provide the input and output files as java.io.File objects !!
File inputFile = new File(“document.doc”);
File outputFile = new File(“document.pdf”);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
// close the connection
Pro: Simplest code possible.
Con: The files need to be available in the same machine where the service is running. In other words, the service is capable of converting only the local files, when used like in the above example. Though there are ways to achieve remote file conversions, I feel that they are way too complex !
Usage as a Web Application:
Though not much useful in programming scenarios, the web application provided by JODConverter is still a pretty useful option. It serves as a good demo app to try out various conversions, but more important is the fact that this app facilitates the usage of JODConverter as a web service.
Usage as a Web Service:
The web application provided by JODConverter exposes a web service for file conversions. It’s not a web service using SOAP or REST. It just provides the conversion functionality without much ado.
All it takes to perform the conversion is an HTTP POST request to the web service URL with certain information in the request headers and the contents of the input file as part of the request body. The response body will contain the converted file contents which can be written into an output file.