相關資料
Windows安裝包:LibreOffice_7.0.6_Win_x64.msi
Linux(CentOs)安裝包:LibreOffice_7.0.6_Linux_x86-64_rpm.zip
安裝完成後,後續将使用java代碼調用軟體服務來提供word格式轉換
安裝
windows安裝:輕按兩下,點下一步…(安裝路徑不要含有中文)
Linux安裝
解壓上面資料的zip,得到兩個tar壓縮檔案,上傳至Linux

安裝
# 解壓
tar -zxvf LibreOffice_7.0.6_Linux_x86-64_rpm.tar.gz
tar -zxvf LibreOffice_7.0.6_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
# 使用yum安裝rpm目錄裡的rpm
yum install LibreOffice_7.0.6.2_Linux_x86-64_rpm/RPMS/*.rpm -y
yum install LibreOffice_7.0.6.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm -y
# 修改profile添加環境變量
vim /etc/profile
# 末尾追加
export LibreOffice_PATH=/opt/libreoffice7.0/program
export PATH=$LibreOffice_PATH:$PATH
# 使配置檔案生效
source /etc/profile
# 列印安裝路徑(下列代碼libreOfficePath填此值)
whereis libreoffice
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
編碼調用部分
pom.xml
<libreoffice.version>6.4.7</libreoffice.version>
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>3.0-beta-4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/jodconverter-3.0-beta-4.jar</systemPath>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>ridl</artifactId>
<version>${libreoffice.version}</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>juh</artifactId>
<version>${libreoffice.version}</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>jurt</artifactId>
<version>${libreoffice.version}</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>unoil</artifactId>
<version>${libreoffice.version}</version>
</dependency>
utils
import lombok.extern.slf4j.Slf4j;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import java.io.File;
/**
* @author 954L
* @create 2021/6/1 13:44
*/
@Slf4j
public class WordUtils {
private static String libreOfficePath;
static {
try {
String osName = System.getProperty("os.name").toLowerCase();
libreOfficePath = osName.contains("windows") ? "E:\\libreOffice\\install": "/usr/bin/libreoffice7.0";
} catch (Exception e) {
log.error("初始化libreOfficePath出錯", e);
}
}
public static void main(String[] args) {
String wordNameRealPath = "D:\\download\\1.docx";
String pdfNameRealPath = "D:\\download\\1.pdf";
docToPdfLibre(wordNameRealPath, pdfNameRealPath);
System.out.println("轉換完成!");
}
public static void docToPdfLibre(String docxPath, String pdfPath) {
OfficeManager officeManager = getOfficeManager();
try {
// 轉換
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File inputFile = new File(docxPath);
File outputFile = new File(pdfPath);
converter.convert(inputFile, outputFile);
} finally {
officeManager.stop();
}
}
private static OfficeManager getOfficeManager() {
OfficeManager officeManager = null;
try {
System.out.println("嘗試連接配接已啟動的服務...");
ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
externalProcessOfficeManager.setConnectOnStart(true);
externalProcessOfficeManager.setPortNumber(8100);
officeManager = externalProcessOfficeManager.buildOfficeManager();
officeManager.start();
System.out.println("轉換服務啟動成功!");
} catch (Exception e) {
System.out.println("啟動新服務!");
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome(new File(libreOfficePath));
configuration.setPortNumber(8100);
// 設定任務執行逾時時間
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);
// 設定任務隊列逾時時間
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
officeManager = configuration.buildOfficeManager();
officeManager.start();
System.out.println("服務啟動成功!");
}
return officeManager;
}
}