Word转PDF并导出–Windows环境实现
一、制作word模板,这里需要文件后缀默认是.docx,${xxxx}是需要替换的内容
二、添加poi所需要的jar包文件,这里使用maven进行jar包管理
jacob jar包下载地址:https://sourceforge.net/projects/jacob-project/?source=typ_redirect
<!--PDF-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.19</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/jacob.jar</systemPath>
</dependency>
<!--PDF-->
三、添加导出word,word转pdf 的工具类,(这里暂时忽略图片的功能,后期会总结添加!!!)
package com.yumc.rdf.common.util;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.apache.poi.xwpf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author: xzh
* @Description: word 导出工具类 / 转 PDF
* @Date: 2022/7/7
*/
public class WordUtils {
/**
* 用于判断导出的word是否转pdf
*/
public static int judgment = 0;
public WordUtils() {
judgment = 0;
}
/**
* 根据模板生成word
*
* @param path 模板的路径
* @param params 需要替换的参数
* @param tableList 需要插入的参数
* @param fileName 生成word文件的文件名
* @param response
*/
public void getWord(String path, Map<String, Object> params, List<String[]> tableList, String fileName,
HttpServletResponse response) throws Exception {
// String saveRoute = path.substring(0, path.indexOf("\\"));
File file = new File(path);
InputStream is = new FileInputStream(file);
CustomXWPFDocument doc = new CustomXWPFDocument(is);
this.replaceInPara(doc, params); // 替换文本里面的变量
this.replaceInTable(doc, params, tableList); // 替换表格里面的变量
OutputStream os = null;
if (judgment == 0) {
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
os = new FileOutputStream( "C:/Users/86159/Desktop/" + fileName);
doc.write(os);
String pdf = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; // 截掉
wToPdfChange("C:/Users/86159/Desktop/" + fileName, "C:/Users/86159/Desktop/" + pdf);
try {
System.out.println("========================pdf下载开始========================");
// path是指欲下载的文件的路径。
file = new File("C:/Users/86159/Desktop/" + pdf);
// 取得文件名。
String filename = URLEncoder.encode(file.getName(), "utf-8");
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
is = new BufferedInputStream(new FileInputStream("C:/Users/86159/Desktop/" + pdf));
byte[] buffer = new byte[is.available()];
is.read(buffer);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
response.addHeader("Content-Length", "" + file.length());
os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);
os.flush();
System.out.println("========================pdf下载结束========================");
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
os = response.getOutputStream();
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
os = new FileOutputStream("C:/Users/86159/Desktop/" + fileName);
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1") );
doc.write(os);
judgment = 0;
}
this.close(os);
this.close(is);
}
/**
* word 转 pdf
*
* @param wordFile word 的路径 word 的路径
* @param pdfFile pdf 的路径
*/
public static void wToPdfChange(String wordFile, String pdfFile) {
ActiveXComponent app = null;
Dispatch document = null;
System.out.println("========================开始转换========================");
try {
// 打开word
System.out.println("开始打开word");
app = new ActiveXComponent("Word.Application");
// 获得word中所有打开的文档
Dispatch documents = app.getProperty("Documents").toDispatch();
System.out.println("打开文件: " + wordFile);
// 打开文档
document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
File target = new File(pdfFile);
if (target.exists()) {
target.delete();
}
System.out.println("另存为: " + pdfFile);
Dispatch.call(document, "SaveAs", pdfFile, 17);
} catch (Exception e) {
System.out.println("转换失败" + e.getMessage());
} finally {
// 关闭office
// app.invoke("Quit", 0);
if (document != null) {
// 关闭文档
Dispatch.call(document, "Close", false);
}
if (app != null) {
app.invoke("Quit", 0);
}
// 获取系统类型
String osName = System.getProperty("os.name");
// 判断是系统类型
if (osName.toLowerCase().startsWith("win")) {
System.out.println(osName);
// window系统
String killCmd = "taskkill /f /im wps.exe";
String killCmd1 = "taskkill /f /im wpscenter.exe";
Process p;
try {
p = Runtime.getRuntime().exec(killCmd);
p = Runtime.getRuntime().exec(killCmd1);
int runnngStatus = p.waitFor();
System.out.println("已杀" + runnngStatus);
} catch (IOException e) {
e.printStackTrace();
System.out.println("转换失败" + e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("转换失败" + e.getMessage());
}
}
}
System.out.println("========================转换结束========================");
}
/**
* 替换段落里面的变量
* @param doc 要替换的文档
* @param params 参数
*/
private void replaceInPara(CustomXWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
this.replaceInPara(para, params, doc);
}
}
/**
* 替换段落里面的变量
*
* @param para 要替换的段落
* @param params 参数
*/
private void replaceInPara(XWPFParagraph para, Map<String, Object> params, CustomXWPFDocument doc) {
List<XWPFRun> runs;
Matcher matcher;
if (this.matcher(para.getParagraphText()).find()) {
runs = para.getRuns();
int start = -1;
int end = -1;
String str = "";
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = runs.get(i);
String runText = run.toString();
if ('$' == runText.charAt(0) && '{' == runText.charAt(1)) {
start = i;
}
if ((start != -1)) {
str += runText;
}
if ('}' == runText.charAt(runText.length() - 1)) {
if (start != -1) {
end = i;
break;
}
}
}
for (int i = start; i <= end; i++) {
para.removeRun(i);
i--;
end--;
}
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
if (str.indexOf(key) != -1) {
Object value = entry.getValue();
if (value instanceof String) {
str = str.replace(key, value.toString());
para.createRun().setText(str, 0);
break;
} else if (value instanceof Map) {
str = str.replace(key, "");
Map pic = (Map) value;
int width = Integer.parseInt(pic.get("width").toString());
int height = Integer.parseInt(pic.get("height").toString());
int picType = getPictureType(pic.get("type").toString());
byte[] byteArray = (byte[]) pic.get("content");
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
try {
//int ind = doc.addPicture(byteInputStream,picType);
//doc.createPicture(ind, width , height,para);
doc.addPictureData(byteInputStream, picType);
// doc.createPicture(doc.getAllPictures().size() - 1, width, height, para);
para.createRun().setText(str, 0);
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
/**
* 为表格插入数据,行数不够添加新行
*
* @param table 需要插入数据的表格
* @param tableList 插入数据集合
*/
private static void insertTable(XWPFTable table, List<String[]> tableList) {
//创建行,根据需要插入的数据添加新行,不处理表头
for (int i = 0; i < tableList.size(); i++) {
XWPFTableRow row = table.createRow();
}
//遍历表格插入数据
List<XWPFTableRow> rows = table.getRows();
int length = table.getRows().size();
for (int i = 1; i < length - 1; i++) {
XWPFTableRow newRow = table.getRow(i);
List<XWPFTableCell> cells = newRow.getTableCells();
for (int j = 0; j < cells.size(); j++) {
XWPFTableCell cell = cells.get(j);
String s = tableList.get(i - 1)[j];
cell.setText(s);
}
}
}
/**
* 替换表格里面的变量
*
* @param doc 要替换的文档
* @param params 参数
*/
private void replaceInTable(CustomXWPFDocument doc, Map<String, Object> params, List<String[]> tableList) {
Iterator<XWPFTable> iterator = doc.getTablesIterator();
XWPFTable table;
List<XWPFTableRow> rows;
List<XWPFTableCell> cells;
List<XWPFParagraph> paras;
while (iterator.hasNext()) {
table = iterator.next();
if (table.getRows().size() > 1) {
//判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
if (this.matcher(table.getText()).find()) {
rows = table.getRows();
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
paras = cell.getParagraphs();
for (XWPFParagraph para : paras) {
this.replaceInPara(para, params, doc);
}
}
}
} else {
insertTable(table, tableList); //插入数据
}
}
}
}
/**
* 正则匹配字符串
*
* @param str
* @return
*/
private Matcher matcher(String str) {
Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
}
/**
* 将输入流中的数据写入字节数组
*
* @param in
* @return
*/
public static byte[] inputStream2ByteArray(InputStream in, boolean isClose) {
byte[] byteArray = null;
try {
int total = in.available();
byteArray = new byte[total];
in.read(byteArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isClose) {
try {
in.close();
} catch (Exception e2) {
e2.getStackTrace();
}
}
}
return byteArray;
}
/**
* 关闭输入流
*
* @param is
*/
private void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
*
* @param os
*/
private void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、在第三步,word转pdf文件需要添加一个jacob.jar包,可以将jacob.jar包放入项目中的resources 文件下的lib中,然后maven通过项目路径引入本地jar包
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PnM84k3i-1658302114459)(C:\Users\86159\AppData\Roaming\Typora\typora-user-images\image-20220720135523781.png)]
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.19</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/jacob.jar</systemPath>
</dependency>
同时需要将jacob中的dll文件放到自己的jdk环境中,将 dll 文件拷贝到本地 jre/bin路径下
五、最后进行测试就可以啦
/**
* 导出订单 PDF
* @param request
* @param response
* @param orderCode
*/
@Override
public void exportOrderInfo(HttpServletRequest request, HttpServletResponse response, String orderCode){
WordUtils wordUtil=new WordUtils();
//获取订单信息
OrderDetailVO order = orderMapper.queryOrderDetail(orderCode);
//动态添加费用详情 测试用例
List<OrderCostDetailVO> costList = new ArrayList<>();
OrderCostDetailVO cost = new OrderCostDetailVO();
cost.setNumber("2");
cost.setContent("测试服务内容");
cost.setTotalHour(BigDecimal.valueOf(20));
cost.setSupplierUnitPrice(BigDecimal.valueOf(2440));
cost.setOrderAmount(BigDecimal.valueOf(48800));
cost.setRemark("测试可否动态添加费用详情");
costList.add(cost);
Map<String, Object> params = new HashMap<String, Object>();
params.put("${firstPartyName}", order.getFirstPartyName());
params.put("${secondPartyName}", order.getSupplierName());
params.put("${pmName}", order.getFirstPmName());
params.put("${supplierName}", order.getSupplierName());
params.put("${place}", order.getDeliveryPlace());
params.put("${orderEffectiveDate}", TimeUtil.getTimeInFormat(order.getOrderEffectiveDate(), "yyyy-MM-dd"));
if(order.getOrderType() == 0){
params.put("${systemName}",order.getSystemName());
params.put("${tax}", "增值税专用发票" + order.getTax().toString()+"%");
}else if(order.getOrderType() == 1){
params.put("${systemName}",order.getSystemName());
params.put("${tax}", "增值税电子普通发票"+ order.getTax().toString()+"%");
}else{
params.put("${systemName}",order.getDemandName());
params.put("${tax}", "普通发票"+ order.getTax().toString()+"%");
}
params.put("${orderAmount}",order.getOrderAmount().toString());
params.put("${totalAmount}",order.getOrderAmount().toString());
params.put("${totalHour}",order.getTotalHour().toString());
try{
List<String[]> testList = new ArrayList<String[]>();
testList.add(new String[]{"1",order.getSystemName(),order.getSupplierUnitPrice().toString(),order.getTotalHour().toString()
,order.getOrderAmount().toString(),order.getDemandDescription()});
for (OrderCostDetailVO detail:costList) {
testList.add(new String[]{detail.getNumber(), detail.getContent(), detail.getSupplierUnitPrice().toString(),detail.getTotalHour().toString(),
detail.getOrderAmount().toString(), detail.getRemark()});
}
String path="C:/Users/86159/Desktop/srb-test.docx"; //模板文件位置
String fileName= new String((orderCode+".docx").getBytes("UTF-8"),"iso-8859-1"); //生成word文件的文件名
wordUtil.getWord(path,params,testList,fileName,response);
}catch(Exception e){
e.printStackTrace();
}
}
六、最后生成的PDF文档
Word转PDF并导出–Linux环境实现
模板共用上面的模板啦
centos安装
yum install libreoffice-headless
yum -y install libreoffice-writer
#测试安装是否成功
libreoffice -version
使用libreoffice把word转pdf
soffice --convert-to pdf:writer_pdf_Export [待转word文件] --outdir [转换pdf文件存放路径]
Java后端代码实现
sevice业务层实现:
/**
* 导出订单 PDF
* @param request
* @param response
* @param orderCode
*/
@Override
public void exportOrderInfo(HttpServletRequest request, HttpServletResponse response, String orderCode){
WordUtils wordUtil=new WordUtils();
//获取订单详情
OrderDetailVO orderDetail = orderMapper.queryOrderDetail(orderCode);
SupplierPO supplier = supplierMapper.findOnlyOne(orderDetail.getSecondPartyCode());
ActualHourVO hour = new ActualHourVO();
if(Objects.nonNull(supplier)) {
if (orderDetail.getOrderType() == 0) {
//获取合计结算工时(人天)
if(orderDetail.getOrderAmount()!=null){
orderDetail.setTotalHour(orderDetail.getOrderAmount().divide(supplier.getSupplierUnitPrice(),4));
}
} else {
hour = zentaoTaskMapper.queryHourByDemand(orderDetail.getZentaoDemandId(), supplier.getSupplierId());
if(hour.getSettleHour()!=null){
BigDecimal totalHour = hour.getSettleHour().divide(new BigDecimal(8),4);
orderDetail.setTotalHour(totalHour);
}
}
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("${orderCode}", orderDetail.getOrderCode());
params.put("${firstPartyName}", orderDetail.getFirstPartyName());
params.put("${secondPartyName}", orderDetail.getSupplierName());
params.put("${firstPmName}", orderDetail.getFirstPmName());
params.put("${secondPmName}", orderDetail.getSecondPmName());
params.put("${place}", orderDetail.getDeliveryPlace());
params.put("${orderEffectiveDate}", TimeUtil.getTimeInFormat(orderDetail.getOrderEffectiveDate(), "yyyy-MM-dd"));
if(orderDetail.getOrderType() == 0){
params.put("${systemName}",orderDetail.getSystemName());
params.put("${tax}", "增值税专用发票" + orderDetail.getTax().toString()+"%");
}else if(orderDetail.getOrderType() == 1){
params.put("${systemName}",orderDetail.getDemandName());
params.put("${tax}", "增值税电子普通发票"+ orderDetail.getTax().toString()+"%");
}else{
params.put("${systemName}",orderDetail.getDemandName());
params.put("${tax}", "普通发票"+ orderDetail.getTax().toString()+"%");
}
params.put("${orderAmount}",orderDetail.getOrderAmount().toString());
params.put("${totalAmount}",orderDetail.getOrderAmount().toString());
try{
List<String[]> testList = new ArrayList<String[]>();
testList.add(new String[]{"1",orderDetail.getOrderType()==0?orderDetail.getSystemName():orderDetail.getDemandName(),orderDetail.getSupplierUnitPrice().toString(),orderDetail.getTotalHour().toString()
,orderDetail.getOrderAmount().toString(),orderDetail.getDemandDescription()});
String path="src/main/resources/templates/order_template.docx"; //模板文件位置
// String path="/opt/project/rdf/srb-test.docx"; //模板文件位置
response.setCharacterEncoding("utf-8");
String fileName= new String((orderCode+".docx").getBytes("UTF-8"),"iso-8859-1"); //生成word文件的文件名
wordUtil.getWord(path,params,testList,fileName,response);
}catch(Exception e){
e.printStackTrace();
}
}
首先根据模板生成word文档
public void getWord(String path, Map<String, Object> params, List<String[]> tableList, String fileName,
HttpServletResponse response) throws Exception {
File file = new File(path);
InputStream is = new FileInputStream(file);
CustomXWPFDocument doc = new CustomXWPFDocument(is);
this.replaceInPara(doc, params); // 替换文本里面的变量
this.replaceInTable(doc, params, tableList); // 替换表格里面的变量
OutputStream os = null;
if (judgment == 0) {
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
os = new FileOutputStream( "/opt/project/rdf/" + fileName);
doc.write(os);
String pdf = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; // 截掉
toPdf("/opt/project/rdf/" + fileName, "/opt/project/rdf/");
log.info("========================word转pdf结束========================");
try {
log.info("========================pdf下载开始========================");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(pdf.getBytes(), "ISO8859-1") );
// path是指欲下载的文件的路径。
file = new File("/opt/project/rdf/" + pdf);
log.info("===================下载文件=================" + "/opt/project/rdf/" + pdf);
if (!file.exists()) {
response.sendError(404, "File not found!");
return;
}
long fileLength = file.length();
response.addHeader("Content-Length", String.valueOf(fileLength));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
log.info("inputsream info,bis:{}", bis);
IOUtils.copy(bis, response.getOutputStream());
bis.close();
log.info("========================pdf下载结束========================");
} catch (IOException ex) {
ex.printStackTrace();
}
}else{
os = response.getOutputStream();
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
os = new FileOutputStream("/opt/project/rdf/" + fileName);
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1") );
doc.write(os);
judgment = 0;
}
this.close(os);
this.close(is);
}
将word转PDF
/**
*
* @param filePath 原文件路径 本地路径
* @param targetFolder 目标文件夹
*/
@Async
public void toPdf(String filePath,String targetFolder){
long start=System.currentTimeMillis();
String srcPath = filePath, desPath = targetFolder;
log.info("源文件:"+filePath);
log.info("目标文件夹:"+targetFolder);
String command = "";
String osName = System.getProperty("os.name");
log.info("系统名称:"+osName);
if (osName.contains("Windows")) {
command = "soffice --headless --convert-to pdf --outdir " + desPath + " " + srcPath ;
windowExec(command);
}else {
command = "soffice --convert-to pdf:writer_pdf_Export " + srcPath + " --outdir " + " " + desPath;
LinuxExec(command);
}
long end=System.currentTimeMillis();
log.info("转换文件耗时:"+(end-start)+"毫秒");
}
前端接收后端的流文件后,创建下载url并导出
exportPDF(rows[i].orderCode).then((response: any) => {
console.log("****response", response);
//导出PDF 20221104版本1 by xzh
let blob = new Blob([response.data], {type: 'application/pdf;charset=utf-8'});
let downloadElement = document.createElement("a");
let href = window.URL.createObjectURL(blob); //创建下载的链接
// alert(href);
downloadElement.href = href;
downloadElement.download = rows[i].orderCode; //下载后的文件名,根据需求定义
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(href); //释放掉blob对象
}
到这步下载的PDF会出现中文乱码的情况
centos环境需要在/usr/share/fonts路径下下载中文字体,简单做法就是将windows系统路径下c:/windows/fonts路径下的需要的字体(微软雅黑)复制到centos中的fonts路径下就可以啦,这一步需要重启系统和nginx哦!!
今天组件使用小结,还有图片格式未实现,继续加油吖~