java中word转换pdf的常用用法
1、poi
poi是apache下的一个java类库,可以帮助我们实现java与各种office格式文件的互相转换。(不推荐,只能用于文字的文档,如果有图片和表格则会排版错误)
2、aspose.words
aspose.words for java是一个原生库,为开发人员提供了丰富的功能来创建、编辑和转换 word、pdf、web 文档,而无需在系统上安装 microsoft word 环境。这个工具非常好用,maven上有对应的依赖和jar包,但是转换后会有水印,因为他是(收费的)
3、spire.doc.free
spire.doc for java是一个专业的 word api,它使 java 应用程序能够创建、转换、操作和打印 word文档,而无需依赖 microsoft word。通过使用这个多功能库,开发人员可以轻松处理大量任务,例如插入图像、超链接、 数字签名、书签和水印、设置页眉和页脚、创建表格、设置背景图像以及添加脚注和尾注。这个跟aspose功能感觉有点差不多,也很好用,但是收费比对还是aspose更好用,而且这个也是收费的
4、documents4j
github:https://github.com/documents4j/documents4j
documents4j 是一个跨平台的文档转换库,并且可以在 linux 上进行 word 转 pdf 的操作。这个比较推荐,开源而且转换后也不会有格式错误(推荐)
documents4j用法
注意windows和linux系统的代码不一样
1.windows系统用法
1.导入依赖,本地必须要有wps或者微软的office
<!--word转换为pdf文档-->
<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>2.编写转换代码(这里我都是传入input输出output然后根据流自己操作)
package com.daysuns.dmas.module.testreportwd.util;
import com.documents4j.api.documenttype;
import com.documents4j.api.iconverter;
import com.documents4j.job.localconverter;
import lombok.extern.slf4j.slf4j;
import java.io.*;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths;
import java.nio.file.standardcopyoption;
@slf4j
public class documents4jutil {
/**
* word转pdf
*
*/
public static void convertwordtopdf(inputstream stream,bytearrayoutputstream sourceoutput) {
string os = system.getproperty("os.name").tolowercase();
log.info("convertwordtopdf 当前操作系统:{}", os);
if (os.contains("win")) {
// windows操作系统
windowswordtopdf(stream,sourceoutput);
} else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
// unix/linux/mac操作系统
linuxwordtopdf(stream,sourceoutput);
} else {
// 未知操作系统
throw new runtimeexception("不支持当前操作系统转换文档。");
}
}
/**
* 通过documents4j 实现word转pdf -- windows 环境 需要有 microsoft office 服务
*
*/
public static void windowswordtopdf(inputstream stream, bytearrayoutputstream sourceoutput) {
try{
iconverter converter = localconverter.builder().build();
converter.convert(stream)
.as(documenttype.docx)
.to(sourceoutput)
.as(documenttype.pdf).execute();
} catch (exception e) {
log.error("winwordtopdf windows环境word转换为pdf时出现异常:", e);
}
}
/**
* 通过libreoffice 实现word转pdf -- linux 环境 需要有 libreoffice 服务
*
*/
public static void linuxwordtopdf(inputstream stream,bytearrayoutputstream sourceoutput) {
// 创建临时文件
file tempfile = createtempfilefrominputstream(stream);
// 构建libreoffice的命令行工具命令
string command = "libreoffice6.4 --headless --invisible --convert-to pdf " + tempfile.getabsolutepath() + " --outdir " + tempfile.getparent();
// 执行转换命令
try {
if (!executelinuxcmd(command)) {
throw new ioexception("转换失败");
}
readpdffiletobytearrayoutputstream(tempfile,sourceoutput);
} catch (exception e) {
log.error("convertwordtopdf: linux环境word转换为pdf时出现异常:"+e +tempfile.getpath());
// 清理临时文件
tempfile.delete();
} finally {
file pdffile = new file(tempfile.getparent(), tempfile.getname().replace(".docx", ".pdf"));
//清理转换后的pdf文件
pdffile.delete();
// 清理临时文件,无论是否成功转换
tempfile.delete();
}
}
/**
* 执行命令行
*
* @param cmd 命令行
* @return
* @throws ioexception
*/
private static boolean executelinuxcmd(string cmd) throws ioexception {
process process = runtime.getruntime().exec(cmd);
try {
process.waitfor();
} catch (interruptedexception e) {
log.error("executelinuxcmd 执行linux命令异常:", e);
thread.currentthread().interrupt();
return false;
}
return true;
}
/**
*
* 创建临时文件
*/
private static file createtempfilefrominputstream(inputstream inputstream) {
try {
file tempfile = file.createtempfile("temp_word", ".docx");
files.copy(inputstream, tempfile.topath(), standardcopyoption.replace_existing);
return tempfile;
} catch (ioexception e) {
log.error("创建临时文件失败:", e);
throw new runtimeexception("创建临时文件失败", e);
}
}
/**
* 读取pdf文件
*/
private static void readpdffiletobytearrayoutputstream(file tempfile,bytearrayoutputstream sourceoutput){
try {
path outputfile = paths.get(tempfile.getparent(), tempfile.getname().replace(".docx", ".pdf"));
files.copy(outputfile, sourceoutput);
} catch (exception e) {
throw new runtimeexception(e);
}
}
}
2.linux系统用法
linux操作系统要安装libreoffice6,原因是documents4j调用的是office的api
1.在线安装
sudo yum install libreoffice (这里建议安装低版本,高版本要求服务器的很多对应api库版本要求也比较高)
2.离线安装(点击后选择版本下载rpm包,上传至linux系统)

3.解压文件

4.进入两个文件夹安装rpm包
使用安装命令
cd libreoffice_6.4.2_linux_x86-64_rpm/rpms yum localinstall *.rpm
安装完成查看版本
which libreoffice6.4 --》显示路径说明安装成功 ll /usr/bin/libreoffice6.4 --》得到 "/opt/libreoffice6.4/program/soffice",说明安装到了 "/opt/libreoffice6.4"
5.脚本测试是否可以成功转换
libreoffice --headless --convert-to pdf 1.doc --》去/temp 临时目录查看,也可以指定文件目录导出
6.同样适用上面代码成功转换文档
package com.daysuns.dmas.module.testreportwd.util;
import com.documents4j.api.documenttype;
import com.documents4j.api.iconverter;
import com.documents4j.job.localconverter;
import lombok.extern.slf4j.slf4j;
import java.io.*;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths;
import java.nio.file.standardcopyoption;
@slf4j
public class documents4jutil {
/**
* word转pdf
*
*/
public static void convertwordtopdf(inputstream stream,bytearrayoutputstream sourceoutput) {
string os = system.getproperty("os.name").tolowercase();
log.info("convertwordtopdf 当前操作系统:{}", os);
if (os.contains("win")) {
// windows操作系统
windowswordtopdf(stream,sourceoutput);
} else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
// unix/linux/mac操作系统
linuxwordtopdf(stream,sourceoutput);
} else {
// 未知操作系统
throw new runtimeexception("不支持当前操作系统转换文档。");
}
}
/**
* 通过documents4j 实现word转pdf -- windows 环境 需要有 microsoft office 服务
*
*/
public static void windowswordtopdf(inputstream stream, bytearrayoutputstream sourceoutput) {
try{
iconverter converter = localconverter.builder().build();
converter.convert(stream)
.as(documenttype.docx)
.to(sourceoutput)
.as(documenttype.pdf).execute();
} catch (exception e) {
log.error("winwordtopdf windows环境word转换为pdf时出现异常:", e);
}
}
/**
* 通过libreoffice 实现word转pdf -- linux 环境 需要有 libreoffice 服务
*
*/
public static void linuxwordtopdf(inputstream stream,bytearrayoutputstream sourceoutput) {
// 创建临时文件
file tempfile = createtempfilefrominputstream(stream);
// 构建libreoffice的命令行工具命令
string command = "libreoffice6.4 --headless --invisible --convert-to pdf " + tempfile.getabsolutepath() + " --outdir " + tempfile.getparent();
// 执行转换命令
try {
if (!executelinuxcmd(command)) {
throw new ioexception("转换失败");
}
readpdffiletobytearrayoutputstream(tempfile,sourceoutput);
} catch (exception e) {
log.error("convertwordtopdf: linux环境word转换为pdf时出现异常:"+e +tempfile.getpath());
// 清理临时文件
tempfile.delete();
} finally {
file pdffile = new file(tempfile.getparent(), tempfile.getname().replace(".docx", ".pdf"));
//清理转换后的pdf文件
pdffile.delete();
// 清理临时文件,无论是否成功转换
tempfile.delete();
}
}
/**
* 执行命令行
*
* @param cmd 命令行
* @return
* @throws ioexception
*/
private static boolean executelinuxcmd(string cmd) throws ioexception {
process process = runtime.getruntime().exec(cmd);
try {
process.waitfor();
} catch (interruptedexception e) {
log.error("executelinuxcmd 执行linux命令异常:", e);
thread.currentthread().interrupt();
return false;
}
return true;
}
/**
*
* 创建临时文件
*/
private static file createtempfilefrominputstream(inputstream inputstream) {
try {
file tempfile = file.createtempfile("temp_word", ".docx");
files.copy(inputstream, tempfile.topath(), standardcopyoption.replace_existing);
return tempfile;
} catch (ioexception e) {
log.error("创建临时文件失败:", e);
throw new runtimeexception("创建临时文件失败", e);
}
}
/**
* 读取pdf文件
*/
private static void readpdffiletobytearrayoutputstream(file tempfile,bytearrayoutputstream sourceoutput){
try {
path outputfile = paths.get(tempfile.getparent(), tempfile.getname().replace(".docx", ".pdf"));
files.copy(outputfile, sourceoutput);
} catch (exception e) {
throw new runtimeexception(e);
}
}
}
总结
到此这篇关于java将word转换成pdf的常用用法的文章就介绍到这了,更多相关java将word转换pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论