我试图让 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行时出现以下错误:-
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
Run Code Online (Sandbox Code Playgroud)
我尝试过的事情:- - 更改端口号 - 研究是否可以找到 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>
Run Code Online (Sandbox Code Playgroud)
我已经在 C:\Program Files\LibreOffice 中安装了 Libre(全新安装)
得到它的工作。这是一个解决方案:-
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);
}
}
Run Code Online (Sandbox Code Playgroud)
是的,这是我的错。JODConverter 文档需要一些重大改进。我创建了静态 JodConverter.convert 方法来简化与 jodconverter 库的交互,但是文档中没有任何地方我明确声明这个静态类将使用一个办公室管理器,该管理器将被创建为所有文档的默认管理器转换器。
这是在创建办公室经理时使用“安装”功能完成的。
所以我感谢你是一个如此聪明的编码员,想通了这一点,这篇 stackoverflow 帖子肯定会帮助许多开发人员!!
| 归档时间: |
|
| 查看次数: |
3964 次 |
| 最近记录: |