JODConverter告诉我需要办公室经理才能构建转换器

时间:2018-09-22 19:17:59

标签: java jodconverter

我正在尝试让JODConverter在带有Jdk 1.8.0_144的Window 10上工作。从代码中可以看到,我认为这可能是时间问题,因此会延迟。如您所见,JODConverter认为OfficeManager正在运行。我正在使用以下代码:

import java.io.File;
import org.jodconverter.JodConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.jodconverter.office.OfficeUtils;
import org.jodconverter.process.ProcessManager;

public class JodConverterTest {

    public static void main(String[] args) throws OfficeException, InterruptedException {

        OfficeManager officeManager
                = LocalOfficeManager.builder()
                        .officeHome("C:\\Program Files\\LibreOffice")
                        .portNumbers(2372)
                        .build();
        officeManager.start();

        File inputFile = new File("c:\\test\\rtf.rtf");
        File outputFile = new File("c:\\test\\rtf.pdf");

        try {

            System.out.println("officeManager.isRunning()="+officeManager.isRunning());
            Thread.sleep(10000);
            System.out.println("officeManager.isRunning()="+officeManager.isRunning());
            JodConverter.convert(inputFile).to(outputFile).execute();
        } finally {
            // Stop the office process
            OfficeUtils.stopQuietly(officeManager);
        }
    }
}

运行它时出现以下错误:-

officeManager.isRunning()=true
officeManager.isRunning()=true
Exception in thread "main" java.lang.IllegalStateException: An office manager is required in order to build a converter.
    at org.jodconverter.job.AbstractConverter.<init>(AbstractConverter.java:57)
    at org.jodconverter.LocalConverter.<init>(LocalConverter.java:93)
    at org.jodconverter.LocalConverter.<init>(LocalConverter.java:49)
    at org.jodconverter.LocalConverter$Builder.build(LocalConverter.java:202)
    at org.jodconverter.LocalConverter.make(LocalConverter.java:73)
    at org.jodconverter.JodConverter.convert(JodConverter.java:48)
    at ZPlaying.JodConverterTest.main(JodConverterTest.java:30)
------------------------------------------------------------------------
BUILD FAILURE

我尝试过的事情: -更改端口号 -研究以查看是否可以找到java Process Manager的类路径并添加以下内容,但由于我对此并不了解,所以找不到ProcessManager的类路径:     .processManager(“ com.example.foo.CustomProcessManager”) -还想知道是否与通过Netbeans运行有关?

以下是适用的maven依赖项:-

        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.1.1</version>
        </dependency>

我已经在C:\ Program Files \ LibreOffice中安装了Libre(全新安装)

2 个答案:

答案 0 :(得分:4)

开始工作。这是一个解决方案:-

package ZPlaying;

import java.io.File;
import org.jodconverter.JodConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.jodconverter.office.OfficeUtils;

public class JodConverterTest {

    public static void main(String[] args) throws OfficeException, InterruptedException {
        OfficeManager officeManager = LocalOfficeManager.builder()
                .install()
                .officeHome("C:\\Program Files\\LibreOffice")
                .build();
        File inputFile = new File("c:\\test\\rtf.rtf");
        File outputFile = new File("c:\\test\\rtf.pdf");
        try {
            // Start an office process and connect to the started instance (on port 2002).
            officeManager.start();
            // Convert
            JodConverter
                    .convert(inputFile)
                    .to(outputFile)
                    .execute();
        } finally {
            // Stop the office process
            OfficeUtils.stopQuietly(officeManager);
        }
    }

答案 1 :(得分:3)

是的,这是我的错。 JODConverter文档需要一些重大改进。我创建了静态JodConverter.convert方法来简化与jodconverter库的交互,但是在文档中没有任何地方可以明确指出此静态类将使用将被创建为所有文档的默认管理器的Office管理器。转换器。

这是在创建办公室经理时使用“安装”功能完成的。

因此,我感谢您成为这么一个聪明的编码人员,发现了这个问题,该stackoverflow帖子肯定会帮助许多开发人员!