Converting Documents with openoffice to Differrent Formats in Java

There is lots of requirements in most commercial projects to convert documents in various formats . For example

HTML —–> Pdf
Doc —–> Pdf
PPT —–> Images

etc

Openoffice has created a very versatile office software which is opensource and can be used in conversion of documents from one format to another

Here is one such example that Converts HTML to PDF. I used JodConverter which is one of the best libraries around which wraps complex openoffice interations with simple object oriented methods

/**
 * 
 */
package com.linkwithweb.converter;

import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFamily;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**
 * Prerequisites
 * %OPENOFFICE_HOME%/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
 * 
 * @author Ashwin Kumar
 * 
 */
public class OpenOfficeConverter {
	// connect to an OpenOffice.org instance running on port 8100
	private OpenOfficeConnection connection = null;

	/**
	 * @param oHTMLText
	 * @param oOutputStream
	 */
	public void convertFromHTMLToPDF(String oHTMLText,
			OutputStream oOutputStream) {
		try {
			DocumentFormat inputDocumentFormat = new DocumentFormat("HTML",
					DocumentFamily.TEXT, "text/html", "html");
			inputDocumentFormat.setExportFilter(DocumentFamily.TEXT,
					"HTML 	(StarWriter)");
			DocumentFormat outputDocumentFormat = new DocumentFormat(
					"Portable	Document Format", DocumentFamily.TEXT,
					"application/pdf", "pdf");
			outputDocumentFormat.setExportFilter(DocumentFamily.TEXT,
					"writer_pdf_Export");
			DocumentConverter oDocumentConverter = new OpenOfficeDocumentConverter(
					connection);
			oDocumentConverter.convert(
					new ByteArrayInputStream(oHTMLText.getBytes()),
					inputDocumentFormat, oOutputStream, outputDocumentFormat);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * 
	 */
	public void openConnection() {
		try {
			if (connection == null || !connection.isConnected())
				connection = new SocketOpenOfficeConnection(8100);
			connection.connect();
		} catch (ConnectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	public void openConnection(String host, int port) {
		try {
			if (connection == null || !connection.isConnected())
				connection = new SocketOpenOfficeConnection(host, port);
			connection.connect();
		} catch (ConnectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	public void closeConnection() {
		// close the connection
		try {
			if (connection != null && connection.isConnected())
				connection.disconnect();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

Now to Use above Method Here is sample Code

/**
 * 
 */
package com.linkwithweb.converter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author Ashwin Kumar
 * 
 */
public class HTMLToPDFConverter {

	public static void main(String[] args) {

		String strHindiHTML = "Hello hi tehrere<h1>heading</h1>";
		OpenOfficeConverter oOpenOfficeConverter = new OpenOfficeConverter();
		oOpenOfficeConverter.openConnection();
		OutputStream out = null;
		try {
			out = new FileOutputStream("test.pdf");

			oOpenOfficeConverter.convertFromHTMLToPDF(strHindiHTML, out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable th) {
			// TODO Auto-generated catch block
			th.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		oOpenOfficeConverter.closeConnection();
		/*
		 * ByteArrayOutputStream oByteArrayOutputStream = new ByteArrayOutputStream();
		 * 
		 * ByteArrayInputStream oByteArrayInputStream = new ByteArrayInputStream(
		 * oByteArrayOutputStream.toByteArray());
		 */
	}

}

Below is sample Maven Configuration to Build the project


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.linkwithweb.documents</groupId>
	<artifactId>DocumentConverter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>DocumentConverter</name>
	<description>DocumentConverter</description>
	<dependencies>
		<dependency>
			<groupId>com.artofsolving</groupId>
			<artifactId>jodconverter</artifactId>
			<version>2.2.1</version>
		</dependency>
		
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>		

	</dependencies>

	<build>
		<finalName>DocumentConverter</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.1</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>com.artofsolving</groupId>
				<artifactId>jodconverter-maven-plugin</artifactId>
				<version>2.2.1</version>
				<configuration>
					<sourceDirectory>${basedir}/src/site/resources</sourceDirectory>
					<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
					<include>**/*.odt</include>
					<outputFormat>pdf</outputFormat>
				</configuration>
				<executions>
					<execution>
						<id>convert </id>
						<phase>site</phase>
						<goals>
							<goal>convert</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<properties>
		<org.easymock.version>2.3</org.easymock.version>
		<org.springframework.version>2.5.6</org.springframework.version>
		<commons.lang.version>2.1</commons.lang.version>
		<log4j.version>1.2.14</log4j.version>
		<org.freemarker.version>2.3.15</org.freemarker.version>
		<manifest.file>src/main/resources/META-INF/MANIFEST.MF</manifest.file>
	</properties>
</project>

Attached is sample Project. Rename it to .rar file

DocumentConverter(Rename Extension to .rar)

Advertisements

6 thoughts on “Converting Documents with openoffice to Differrent Formats in Java

  1. Hi! I have installed openoffice.org as a service in a debian server and create a java function as it is mentioned below. I have created a java applet which use the convertFromHTMLToPDF , but I got the next error from the open office server:
    com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: conversion failed: could not load input document at com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter.loadAndExport(OpenOfficeDocumentConverter.java:131) at com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter.convertInternal(OpenOfficeDocumentConverter.java:120) at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:104) at com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter.convertInternal(OpenOfficeDocumentConverter.java:88) at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:82)

    What should be wrong? Thanks and regards. Virag

  2. i created two .java file one is OpenOfficeConverter.java and second is HTMLToPDFConverter.java file and i add the .jar file which is required . but when i run the HTMLToPDFConverter.java class it giving error so please suggest me what i wrong?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s