1、安装libreOffice
2、代码实现
package com.szoa.util.pdf;
import java.io.File;
import java.util.regex.Pattern;
import org.jodconverter.OfficeDocumentConverter;
import org.jodconverter.office.DefaultOfficeManagerBuilder;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PDFConvert {
private static String officeHomeDir = null;
private final static Logger logger = LoggerFactory.getLogger(PDFConvert.class);
/**
*
*@name 文档转换为pdf工具类
*@description 相关说明 支持:xls,xlsx,ppt,pptx,txt,其中doc,docx转换与原文件有较大差异,libreOffice 默认安装路径
*Linux:/opt/libreoffice6.0
*Windows:C:/Program Files (x86)/LibreOffice
*Mac:/Application/openOfficeSoft
*@time 创建时间:2018年9月17日下午1:49:18
*@param sourceFile 需要转换的原文件
*@param tarPdfFile 转换后的目标pdf文件
*@return
*@throws OfficeException
*@author [email protected]
*@history 修订历史(历次修订内容、修订人、修订时间等)
*/
public static String doDocToFdpLibre(String sourceFile, String tarPdfFile) throws OfficeException {
File inputFile = new File(sourceFile);
String libreOfficePath = getOfficeHome();
DefaultOfficeManagerBuilder builder = new DefaultOfficeManagerBuilder();
builder.setOfficeHome(new File(libreOfficePath));
// 端口号
builder.setPortNumber(8100);
builder.setTaskExecutionTimeout(1000 * 60 * 5L);
// 设置任务执行超时为5分钟
builder.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
// 设置任务队列超时为24小时
OfficeManager officeManager = builder.build();
startService(officeManager);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File outputFile = new File(tarPdfFile);
converter.convert(inputFile, outputFile);
stopService(officeManager);
String pdfPath = outputFile.getPath();
return pdfPath;
}
private static String getOfficeHome() {
if (null != officeHomeDir) {
return officeHomeDir;
} else {
String osName = System.getProperty("os.name");
if (Pattern.matches("Windows.*", osName)) {
officeHomeDir = "C:/Program Files (x86)/LibreOffice";
return officeHomeDir;
} else if (Pattern.matches("Linux.*", osName)) {
officeHomeDir = "/opt/libreoffice6.0";
return officeHomeDir;
} else if (Pattern.matches("Mac.*", osName)) {
officeHomeDir = "/Application/openOfficeSoft";
return officeHomeDir;
}
return null;
}
}
private static void stopService(OfficeManager officeManager) throws OfficeException {
if (null != officeManager) {
officeManager.stop();
}
logger.info("关闭office转换成功!");
}
private static void startService(OfficeManager officeManager) {
try {
// 准备启动服务
officeManager.start(); // 启动服务
logger.info("office转换服务启动成功");
} catch (Exception ce) {
logger.error("office转换服务启动失败!详细信息:{}", ce);
}
}
/**
*
*@name 设置libreOffice安装目录
*@description 相关说明:如果libreOffice安装目录为默认目录,则不需要设置,否则需要设置
*@time 创建时间:2018年9月17日下午1:52:36
*@param officeHome
*@author 作者
*@history 修订历史(历次修订内容、修订人、修订时间等)
*/
public static void setOfficeHome(String officeHome) {
officeHomeDir = officeHome;
}
}
3、pom.xml 依赖
4.0.0
com.szoa
szoa-pdf-util
1.0.0
jar
soze-pdf-util
http://maven.apache.org
UTF-8
org.slf4j
slf4j-log4j12
1.8.0-alpha2
org.jodconverter
jodconverter-core
4.0.0-RELEASE
szoa-pdf-util-${project.version}
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.7
1.7
UTF-8
org.apache.maven.plugins
maven-assembly-plugin
2.5.5
com.szoa.util.pdfmain.PdfDomain
jar-with-dependencies
make-assembly
package
single
4、安装libreOffice
安装流程:
rpm安装方式安装
tar -zxvf LibreOffice_6.0.6_Linux_x86-64_rpm.tar.gz
cd LibreOffice_6.0.6
cd LibreOffice_6.0.6.2_Linux_x86-64_rpm/
cd RPMS/
rpm -ivh *.rpm
[root@bogon RPMS]# rpm -ivh *.rpm
Preparing... ########################################### [100%]
1:libreoffice6.0-ure ########################################### [ 2%]
2:libobasis6.0-core ########################################### [ 5%]
3:libobasis6.0-base ########################################### [ 7%]
4:libobasis6.0-impress ########################################### [ 10%]
5:libobasis6.0-writer ########################################### [ 12%]
6:libobasis6.0-calc ########################################### [ 15%]
7:libobasis6.0-draw ########################################### [ 17%]
8:libobasis6.0-en-US ########################################### [ 20%]
9:libobasis6.0-images ########################################### [ 22%]
10:libreoffice6.0 ########################################### [ 24%]
11:libobasis6.0-math ########################################### [ 27%]
12:libobasis6.0-pyuno ########################################### [ 29%]
13:libobasis6.0-librelogo ########################################### [ 32%]
14:libreoffice6.0-math ########################################### [ 34%]
15:libreoffice6.0-base ########################################### [ 37%]
16:libreoffice6.0-calc ########################################### [ 39%]
17:libreoffice6.0-dict-en ########################################### [ 41%]
18:libreoffice6.0-dict-es ########################################### [ 44%]
19:libreoffice6.0-dict-fr ########################################### [ 46%]
20:libreoffice6.0-draw ########################################### [ 49%]
21:libreoffice6.0-en-US ########################################### [ 51%]
22:libreoffice6.0-impress ########################################### [ 54%]
23:libreoffice6.0-writer ########################################### [ 56%]
24:libobasis6.0-ogltrans ########################################### [ 59%]
25:libobasis6.0-postgresql########################################### [ 61%]
26:libobasis6.0-extension-########################################### [ 63%]
27:libobasis6.0-extension-########################################### [ 66%]
28:libobasis6.0-extension-########################################### [ 68%]
29:libobasis6.0-extension-########################################### [ 71%]
30:libobasis6.0-extension-########################################### [ 73%]
31:libobasis6.0-extension-########################################### [ 76%]
32:libobasis6.0-firebird ########################################### [ 78%]
33:libobasis6.0-gnome-inte########################################### [ 80%]
34:libobasis6.0-graphicfil########################################### [ 83%]
35:libobasis6.0-kde-integr########################################### [ 85%]
36:libobasis6.0-onlineupda########################################### [ 88%]
37:libobasis6.0-ooofonts ########################################### [ 90%]
38:libobasis6.0-ooolinguis########################################### [ 93%]
39:libobasis6.0-python-scr########################################### [ 95%]
40:libobasis6.0-xsltfilter########################################### [ 98%]
41:libreoffice6.0-freedesk########################################### [100%]
/usr/bin/update-desktop-database
/usr/bin/update-mime-database
mkdir: 无法创建目录"/usr/share/icons/hicolor/icon-theme.cache": 不是目录
mkdir: 无法创建目录"/usr/share/icons/hicolor/index.theme": 不是目录
/usr/bin/gtk-update-icon-cache
/usr/bin/gtk-update-icon-cache
/usr/bin/update-desktop-database
/usr/bin/update-desktop-database
默认安装目录:/opt/libreoffice6.0