Tags
converter, file formats, Java, jod, openoffice, registry, web, web application, web service, windows service
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);
connection.connect();// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);// close the connection
connection.disconnect();
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.
Hello! Could you expand your example for the a bit not-so-hapy java users like me, and describe how to compile and run your code? That would be awesome, thanks
Pingback: Kay Ramme of Sun has created “ODF@WWW,” an ODF Wiki « Wave A Dead Chicken
hello,i hope to know whether or not i can use jodconverter in my java application without installed openoffice. for example include external .jar.
Because my java application run in the embedded linux and memory limited
JODConverter absolutely requires openoffice. Openoffice is what actually does all the heavy lifting, JODConverter merely feeds it instructions.
Hey
For Office formats I am using JODConvertour2.2.2 jar to convert to PDF formt!
It works well trying to convert one single file! This package will be included as part of a webApplication meaning I cant effort to open a new connection for each conversion (memory and scalability issues…)
How can I create a connection pool? Is there any other known package to convert office documents to pdf? Did you solve the Linux issue?
System requirements –
JRE – 1.5
apache-tomcat-6.0.20
Operating System – windows Xp for development , production – Linux
I will appreciate any help
Thanks, Natalie
I have not tried out connection pooling. You can use the web service that JODConverter provides instead of making a connection everytime. You can make a http post request to the web service which will render back the pdf.
Download the web app that JODConverter provides and run it in a server. This web app then exposes the web service at this url: http://app_url/service.
I am using below code (using JODConverter) to connect to open office and convert files from java web application. Every time the user wants to preview document, below code gets executed. The web application is running on a weblogic on windows server (production server).
connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new
OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
Started open office using – ” soffice -headless -
accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard ”
Problem:
‘soffice.exe’ gets killed constanly and the application is throwing error “Problem to convert Doc/DocX resume. Please contact the support.”. Everytime I have to start soffice manually using above command and it works fine.
So I made soffice.exe as windows service. But still facing the same issue that exe gets killed sometimes and getting error. I have been breaking my heads for a week but no solution yet.
Can you please help??
hello ,i want to ask a question ,whether or not JODConverter can convert doc to docx.
Thank you~