具体的Windows和Linux如何安装openoffice大家可以自行百度,我就不过多赘述了。
下面主要给出word转pdf的工具
pom.xml
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.30version>
dependency>
<dependency>
<groupId>org.openofficegroupId>
<artifactId>juhartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>org.openofficegroupId>
<artifactId>jurtartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>org.openofficegroupId>
<artifactId>ridlartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>org.openofficegroupId>
<artifactId>unoilartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>
PathUtils.java
package com.protectzaizai.schoolcardoa.utils;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @Author: 动感小⑦
* @Date:2020/2/22 10:56
*/
public class PathUtils {
//获取springboot项目的根目录
public static String getRootPath() throws FileNotFoundException {
//获取跟目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) path = new File("");
System.out.println("rootPath:" + path.getAbsolutePath());
return path.getAbsolutePath();
}
//获取springboot项目的static目录
public static String getStaticPath() throws IOException {
//获取static目录 注意部署的时候更改路径/
String path = null;
if (getSeparator().equals("/")) { //linux系统
path = getRootPath() + "/static";
} else {
path = getRootPath() + "\\static";
}
System.out.println("staticPath:" + path);
return path;
}
//跨平台路径斜杠分隔符获取
public static String getSeparator() {
return File.separator;
}
// 获取文件后缀名
public static String getSuffix(String path) {
return path.substring(path.lastIndexOf(".") + 1);
}
}
DocUtils.java
package com.protectzaizai.schoolcardoa.utils;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.protectzaizai.schoolcardoa.entity.locationPath;
import com.sun.star.lib.uno.protocols.urp.urp;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jeecgframework.poi.word.WordExportUtil;
import java.io.*;
import java.net.ConnectException;
import java.util.*;
/**
* @Author: 动感小⑦
* @Date:2020/2/22 11:07
*/
public class DocUtils {
/**
* 记得修改自己的openoffice路径
* 下面host_Str port_Str的一般都是默认 /opt/openoffice4 D:/OpenOffice 4
*/
public static String OpenOffice_HOME = "/opt/openoffice4";
public static String host_Str = "127.0.0.1";
public static String port_Str = "8100";
/**
* word文档转pdf文件
*
* @param sourceFile office文档绝对路径
* @param destFile pdf文件绝对路径
* @return
*/
public static int office2PDF(String sourceFile, String destFile) {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1; // 找不到源文件
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
try {
OpenOfficeConnection connection = initOpenOffice();
if (!outputFile.exists()) {
return convertFile(inputFile, outputFile, connection);
}
// 关闭连接和服务
connection.disconnect();
return -1;
} catch (ConnectException e) {
OpenOfficeConnection connection = initOpenOffice();
try {
return convertFile(inputFile, outputFile, connection);
} catch (ConnectException e1) {
e1.printStackTrace();
}
}
return 1;
}
// 下面是工具方法
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* 初始化启动openoffice服务
*
* @throws IOException
*/
private static OpenOfficeConnection initOpenOffice() {
try {
// 启动OpenOffice的服务
String separator = PathUtils.getSeparator();
String command = null;
if (separator.equals("/")) {
//linux系统
command = OpenOffice_HOME
+ "/program/soffice -headless -accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;\" -nofirststartwizard";
} else {
command = OpenOffice_HOME
+ "/program/soffice.exe -headless -accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;\" -nofirststartwizard";
}
System.out.println("###\n" + command);
Runtime.getRuntime().exec(command);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(Integer.parseInt(port_Str));
return connection;
} catch (Exception e) {
e.printStackTrace();
System.out.println("###\n启动openoffice服务错误");
}
return null;
}
private static int convertFile(File inputFile, File outputFile, OpenOfficeConnection connection) throws ConnectException {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + outputFile.getPath() + "****");
return 0;
}
}
注意:在Windows上可以用,部署到Linux时记得导入字体,linux本身是缺乏一些基本字体的
链接:https://pan.baidu.com/s/1gPwOcSm26My5-HV3NtMWAQ
提取码:b725
这里是字体自己下载就好
到linux的 /usr/share/fonts/ 目录下
把里面的字体文件全部拉进去
然后命令行
# 生成字体索引
sudo mkfontscale
sudo mkfontdir
# 更新字体缓存
sudo fc-cache
这个util亲测可用,有问题可以在评论里说,或者联系QQ:857957495。