java办公文件转pdf
centos 7 安装openoffice
网盘下载:
注意:里面有win10版和centos版 , win10直接安装
链接:https://pan.baidu.com/s/15A6EDQB2IibwDkevAPlA0g
提取码:jie0
1.下载tar.gz包。下载地址:http://www.openoffice.org/zh-cn/ (需要下载rpm格式的)
2.通过xftp上传到linux中。我的目录在/opt/openoffice中
3.解压文件:tar -zxvf Apache_OpenOffice_4.1.7_Linux_x86-64_install-rpm_zh-CN.tar.gz
解压后进入zh-CN目录中:
4. cd RPMS/ 里面都是rpm文件,我们需要安装这些文件
5. 安装rpm文件: rpm -ivh *.rpm
6. 进入desktop-integration/目录:cd desktop-integration/
7.安装openoffice:rpm -ivh openoffice4.1.7-redhat-menus-4.1.7-9790.noarch.rpm
注意:文件夹名称根据实际情况修改
java代码
maven:
<!-- Openoffice-->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>jodconverter-core</artifactId>
<version>1.0.5</version>
</dependency>
注意:自行修改安装目录
工具类:
/**
* @author : xxxx@163.com
* @类名 : OpenOffice2PDF
* @说明 : TODO
* @创建时间 : 2020/9/3 14:55
* @版本 1.0
*/
@Component
public class OpenOffice2PDF {
/**
* 格式
*/
private static final String[] OFFICE_POSTFIXS = {"doc", "docx", "xls", "xlsx", "ppt", "pptx"};
private ArrayList<String> Office_Formats = new ArrayList<String>();
/**
* pdf格式
*/
private static final String PDF_POSTFIX = "pdf";
/**
* 安装目录
*/
public String getOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice4";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:\\Program Files (x86)\\OpenOffice 4";
}
return null;
}
/**
* 转换文件
*
* @param inputFilePath
* @param outputFilePath
* @param converter
*/
public void converterFile(String inputFilePath, String outputFilePath,
OfficeDocumentConverter converter) {
File inputFile = new File(inputFilePath);
File outputFile = new File(outputFilePath);
// 假如目标路径不存在,则新建该路径
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);
System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile
+ "\n成功!");
}
/**
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
*
* @param inputFilePath 源文件路径,如:"e:/test.docx"
* @param outputFilePath 如果指定则按照指定方法,如果未指定(null)则按照源文件路径自动生成目标文件路径,如:"e:/test_docx.pdf"
* @return
*/
public boolean openOffice2Pdf(String inputFilePath, String outputFilePath) {
boolean flag = false;
/*
* 连接OpenOffice.org 并且启动OpenOffice.org
*/
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// 获取OpenOffice.org 3的安装目录
String officeHome = getOfficeHome();
config.setOfficeHome(officeHome);
// 启动OpenOffice的服务
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
// 连接OpenOffice
OfficeDocumentConverter converter = new OfficeDocumentConverter(
officeManager);
long begin_time = new Date().getTime();
File inputFile = new File(inputFilePath);
Collections.addAll(Office_Formats, OFFICE_POSTFIXS);
if (( inputFilePath!=null ) && (inputFile.exists())) {
// 判断目标文件路径是否为空
if (Office_Formats.contains(getPostfix(inputFilePath))) {
if (null == outputFilePath) {
// 转换后的文件路径
String outputFilePath_new = generateDefaultOutputFilePath(inputFilePath);
converterFile(inputFilePath, outputFilePath_new, converter);
flag = true;
} else {
converterFile(inputFilePath, outputFilePath, converter);
flag = true;
}
}
} else {
System.out.println("找不到资源");
}
long end_time = new Date().getTime();
System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms");
officeManager.stop();
return flag;
}
/**
* 如果未设置输出文件路径则按照源文件路径和文件名生成输出文件地址。例,输入为 D:/fee.xlsx 则输出为D:/fee_xlsx.pdf
*/
public String generateDefaultOutputFilePath(String inputFilePath) {
String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), "_" + getPostfix(inputFilePath) + "." + PDF_POSTFIX);
return outputFilePath;
}
/**
* 获取inputFilePath的后缀名
*/
public String getPostfix(String inputFilePath) {
String[] p = inputFilePath.split("\\.");
if (p.length > 0) {
// 比较文件扩展名
return p[p.length - 1];
} else {
return null;
}
}
public static void main(String[] args) {
String str = "E:\\java\\dev\\kitop-cloud2.0\\kitop-resource\\src\\main\\resources\\教育盒子生产步骤说明.docx";
OpenOffice2PDF office2pdf = new OpenOffice2PDF();
// office2pdf.openOffice2Pdf(str, str.replaceAll(".", "") + "." + PDF_POSTFIX);
office2pdf.openOffice2Pdf(str, null);
}
}