直接上结论
1、spire.doc.free 收费不考虑、前4页无水印 30s
2、docx4j 20s
3、liboffice 处理时间1.4s
4、documents4j较快只支持windows micro office
1 Windows 环境WORD 转换
dom4j 只能在windows环境调用microsoft office
引入jar包
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
public static void wordToPdf(MultipartFile file, String outFilePath) {
File outputFile = new File(outFilePath);
InputStream doc = null;
OutputStream outputStream = null;
try {
doc = file.getInputStream();
outputStream = Files.newOutputStream(outputFile.toPath(), StandardOpenOption.CREATE_NEW);
IConverter converter = LocalConverter.builder().build();
//转换docx=>pdf
boolean flag = converter.convert(doc).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
if (flag) {
converter.shutDown();
}
doc.close();
outputStream.close();
System.out.println("文件名:" + outFilePath + " 转换成功!");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件名:" + outFilePath + " 转换失败!");
}
}
2 linux 环境WORD 转换
Dockerfile 配置
FROM openjdk:8-jdk-alpine
MAINTAINER auther<>
COPY start.sh /home/
COPY ./target/demo.jar /home/demo.jar
# 复制字体文件到容器的字体目录
COPY simhei.ttf /usr/share/fonts/
# 设置字体文件的权限
RUN chmod 655 /usr/share/fonts/simhei.ttf
ENV TZ=Asia/Shanghai
# 安装字体库,安装liboffice
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN echo -e 'https://mirrors.aliyun.com/alpine/v3.6/main/\nhttps://mirrors.aliyun.com/alpine/v3.6/community/' > /etc/apk/repositories \
&& apk update \
&& apk upgrade \
&& apk --no-cache add ttf-dejavu fontconfig libreoffice
# 刷新系统字体缓存
RUN fc-cache -fv
#无用处,liboffce调用的上面容器内的字体
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
EXPOSE 8080
ENTRYPOINT ["/bin/sh", "/home/start.sh"]
调用方法
执行路径
libreOfficeCommand = "/usr/lib/libreoffice/program/soffice.bin"
public static int poiWord2PDF(String source, String targetDir, String libreOfficeCommand) {
// libreOfficeCommand Linux可执行文件的路径 source word完整路径 targeDir 则是PDF的存放目录,不带文件名称噢,文件名称会自动取source的文件名称作为名称
String[] cmdArray = {
libreOfficeCommand,
"--headless",
"--convert-to", "pdf:writer_pdf_Export",
source,
"--outdir", targetDir
};
int exitCode = 0;
try {
Process process = Runtime.getRuntime().exec(cmdArray);
// simhei.ttf
// 获取命令执行的输出
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
exitCode = process.waitFor();
System.out.println(("转换码:"+exitCode));
} catch (Exception e) {
System.out.println( "网络繁忙,请重试!");
}
return exitCode;
}