【问题标题】:Issue with JODConverter and running LibreOffice in headless modeJODConverter 出现问题并在无头模式下运行 LibreOffice
【发布时间】:2013-08-16 02:37:30
【问题描述】:

我正在使用以下代码使用 JOD 将 .doc 转换为 .pdf。

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 进行转换吗?

【问题讨论】:

标签: openoffice.org libreoffice jodconverter


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    一种方法是包装你的 cmd 指令

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

    作为 java 进程,请参阅this 关于 SO 的问题。

    解决方案可能是:

     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 命令在 windows 中创建批处理文件或在 linux 中创建 shell 脚本,并将其设置为在 windows 或 linux 登录时自动启动。之后按原样执行您的代码...

    【讨论】:

    • 为什么将 libreoffice 包装在 cmd 中?这应该是多余的。
    猜你喜欢
    • 2017-06-11
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 2018-08-06
    相关资源
    最近更新 更多