Archive for the ‘JODConverter’ Category

Convert a Word Document to PDF Using Java

Thursday, March 20th, 2008
I had a need today to convert a Word Document to a PDF using Java. This needed to be an automated process. A quick Google search was turning up a little empty handed. I kept finding people talking about ‘just open it in OpenOffice and export as a PDF’. Obviously, that doesn’t fit the automated requirement.
 
Apache has a Java API, called POI, that allows you to access Microsoft formats from within Java. It looks handy, but I found some people complaining about the complexity of it. Some further searching turned up mention of something called JODConverter. This is a fantastic project.
 
JODConverter is essentially a Java library that utilizes OpenOffice’s conversion engine to convert to and from any format that OpenOffice supports. This includes Word, Excel, PowerPoint, RTF, Plain Text, etc.
 
I decided to see how easy it was going to be to use this library. Let’s face it, a lot of these open source projects claim to do all kinds of great things, but do not provide the greatest results. So, I created a small test project and wrote a simple class. Here’s what it looks like:
 
public class convertFile {
   public static void main(String[] args) {
      File inputFile = new File("resume-2.doc");
      File outputFile = new File("resume-2.pdf");
     OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
     try {
        connection.connect();
     } catch(Exception e) {
     }

     DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
     converter.convert(inputFile, outputFile);

     connection.disconnect();

   }
}

 

Obviously, there’s error-checking that is lacking. For example, the convert() method should be wrapped to ensure that a failed conversion doesn’t blow up your application, and something should be done if an exception is caught on the connection. However, this is the basic skeleton. Pretty cool.
 
Basically, all this does is open a connection to OpenOffice, create a converter, and pass in the files to convert. JODConverter and OpenOffice handle everything else.
 
The only caveat to this is that OpenOffice must be installed on the server and must be running as a headless app (the JODConverter README file explains how to do this). While that may not be ideal, it’s probably worth it for the amazing results this library provides.
 
Great tool, highly recommended.
 
Joe Koenig
Creative Anvil
Search Engine Optimization & Web Application Development

Share/Save/Bookmark