Transfer Doc to Pdf on Ubuntu System

Transfer Doc to Pdf on Ubuntu System

1. Command
Using command line to transfer doc to pdf
>unoconv -f pdf sprint9_Additional_Information.doc

When I am executing this command with none-root user. I got this error messages:
Error: Unable to connect or start own listener. Aborting.

So, I su to root user, it is ok. It is said that it can also transfer other formats. But I do not have
chance to have a try.
unoconv -f doc other-document.odt
unoconv -f jpg some-image.png
unoconv -f xsl some-spreadsheet.csv

We can use command to see all the supporting format.
>unoconv --show

2. Start the openoffice service and execute in Java class
start the service
>soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

we can get the opensource jar packages here
http://www.artofsolving.com/opensource/jodconverter
http://sourceforge.net/projects/jodconverter/files/

The Java class will be as follow:
package com.chinacreator.test;



import java.io.File;

import java.net.ConnectException;



import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;



public class JOD4DocToPDF {



public void docToPdf(File inputFile, File outputFile) {

long start = System.currentTimeMillis();

// connect to an OpenOffice.org instance running on port 8100

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

connection.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(

connection);

converter.convert(inputFile, outputFile);

} catch (ConnectException e) {

e.printStackTrace();

} finally {

// close the connection

if (connection != null) {

connection.disconnect();

connection = null;

}

}

long end = System.currentTimeMillis();

System.out.println("converted from " + inputFile.getName() + " to "

+ outputFile.getName() + " time:" + (end - start) + " ms.");

}



class TestThread extends java.lang.Thread {

public File inputFile;

public File outputFile;



public void run() {

JOD4DocToPDF job = new JOD4DocToPDF();

job.docToPdf(inputFile, outputFile);

System.out.println(outputFile.getName() + " converting start.");

}

}



public void test() {

TestThread t1 = new TestThread();

t1.inputFile = new File("/var/tmp/c1.doc");

t1.outputFile = new File("/var/tmp/c1.pdf");



TestThread t2 = new TestThread();

t2.inputFile = new File("/var/tmp/c2.doc");

t2.outputFile = new File("/var/tmp/c2.pdf");



t2.start();

t1.start();

}



public static void main(String[] args) throws Exception {

JOD4DocToPDF p = new JOD4DocToPDF();

p.test();

}

}

There is some problem on ubuntu, maybe it is right on windows.

references:
http://ubuntuwriter.blogspot.com/2008/12/pdf.html
http://www.iteye.com/topic/352103

你可能感兴趣的:(socket,ubuntu,F#,OpenSource,XSL)