Issue with JODConverter and running LibreOffice in headless mode

我只是一个虾纸丫 提交于 2019-12-13 01:43:03

问题


I am using the following code to convert .doc to .pdf using JOD.

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

But I have to run

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

separately to start LibreOffice in headless mode.

Is there a way to start LibreOffice programmatically? Or, can't we just give the path to LibreOffice folder to JOD to do the conversion?


回答1:


You don't need JOD at all to convert a doc file to a PDF. This can be directly done with LibreOffice:

libreoffice --headless --convert-to pdf document.doc



回答2:


One way would be to wrap your cmd instruction

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

as java process, see this question on SO.

Solution could be:

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

It is ad-hoc and not tested solution but it might work. Another option is to make batch file in windows or shell script in linux with your cmd command and set it to auto start on windows or linux login. After that execute your code as it is...



来源:https://stackoverflow.com/questions/18264975/issue-with-jodconverter-and-running-libreoffice-in-headless-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!