2013-08-16 41 views
0

我正在使用以下代码将.doc转换为使用JOD的.pdf。JODConverter问题并在无头模式下运行LibreOffice

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(); 

但我不得不跑

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 

分别在无头的模式下启动的LibreOffice。

有没有办法以编程方式启动LibreOffice?或者,我们不能只给路径LibreOffice文件夹JOD做转换吗?

+0

JODconverter 3.0似乎已经开始的LibreOffice如果需要的话,看到http://code.google.com/p/jodconverter/wiki/GettingStarted的一种方式。 –

回答

0

一种方法是来包装你的CMD指令

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 

为java程序,请参见SO this问题。

的解决办法是:

File inputFile = new File("document.doc"); 
File outputFile = new File("document.pdf"); 

String[] args = {"cmd","/c","\\path to libreoffice executable","-headless", "-accept='socket,host=127.0.0.1,port=8100;urp;'", "-nofirststartwizard"}; 

try{ 
    Runtime rt = Runtime.getRuntime(); 
    ProcessBuilder pb = new ProcessBuilder(args); 
    Process pr = pb.start(); 

    // connect to an OpenOffice.org instance running on port 8100 
    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); 
    connection.connect(); 
}catch{Exception e){ 
} 

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

// close the connection 
connection.disconnect(); 

这是即席而不是测试的解决方案,但它可能工作。 另一种选择是使用cmd命令在Linux中的Windows或shell脚本中创建批处理文件,并将其设置为在Windows或Linux登录时自动启动。之后,执行你的代码,因为它是...

+0

为什么你在'cmd'中包装libreoffice?这应该是多余的。 –

0

你根本不需要JOD将doc文件转换为PDF。这可以用LibreOffice中直接完成:

libreoffice --headless --convert-to pdf document.doc 
相关问题