安装部分
-
安装LibreOffice ,有window linux 版本
下载地址 -
window 配置环境变量 , PATH 下添加 LibreOffifce 的安装目录
-
打开cmd ,输入 soffice,
出现该图片说明配置成功
撸代码
想要调用libreoffice 的服务,需要使用jodconverter类库
github地址
- maven依赖
<properties>
<jodconverter.version>4.2.2</jodconverter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>${jodconverter.version}</version>
</dependency>
</dependencies>
- 关键代码
File inputFile = new File("document.doc");
File outputFile = new File("document.pdf");
// Create an office manager using the default configuration.
// The default port is 2002. Note that when an office manager
// is installed, it will be the one used by default when
// a converter is created.
final LocalOfficeManager officeManager = LocalOfficeManager.install();
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);
}