package com.zzg.utils;
|
|
import cn.afterturn.easypoi.word.WordExportUtil;
|
import com.lowagie.text.Document;
|
import com.lowagie.text.pdf.PdfCopy;
|
import com.lowagie.text.pdf.PdfImportedPage;
|
import com.lowagie.text.pdf.PdfReader;
|
import com.zzg.common.exception.GlobalException;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
import org.springframework.util.Assert;
|
|
import javax.imageio.ImageIO;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.awt.image.BufferedImage;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.net.URLEncoder;
|
import java.util.Map;
|
|
/**
|
* @ClassName WordUtil2
|
* @Description 描述:easypoi导出工具类
|
* @Author LIJI
|
* @Date 2021/6/29 15:27
|
* @Version 1.0
|
**/
|
public class WordUtils {
|
/**
|
* 导出word
|
* <p>第一步生成替换后的word文件,只支持docx</p>
|
* <p>第二步下载生成的文件</p>
|
* <p>第三步删除生成的临时文件</p>
|
* 模版变量中变量格式:{{foo}}
|
*
|
* @param templatePath word模板地址
|
* @param temDir 生成临时文件存放地址
|
* @param fileName 文件名
|
* @param params 替换的参数
|
* @param request HttpServletRequest
|
* @param response HttpServletResponse
|
*/
|
public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
|
Assert.notNull(templatePath, "模板路径不能为空");
|
Assert.notNull(temDir, "临时文件路径不能为空");
|
Assert.notNull(fileName, "导出文件名不能为空");
|
Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
|
if (!temDir.endsWith("/")) {
|
temDir = temDir + File.separator;
|
}
|
File dir = new File(temDir);
|
if (!dir.exists()) {
|
dir.mkdirs();
|
}
|
try {
|
String userAgent = request.getHeader("user-agent").toLowerCase();
|
if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
|
fileName = URLEncoder.encode(fileName, "UTF-8");
|
} else {
|
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
|
}
|
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
|
String tmpPath = temDir + fileName;
|
FileOutputStream fos = new FileOutputStream(tmpPath);
|
doc.write(fos);
|
// 设置强制下载不打开
|
response.setContentType("application/force-download");
|
// 设置文件名
|
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
|
OutputStream out = response.getOutputStream();
|
doc.write(out);
|
out.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
delFileWord(temDir, fileName);//这一步看具体需求,要不要删
|
}
|
|
}
|
|
/**
|
* 删除零时生成的文件
|
*/
|
public static void delFileWord(String filePath, String fileName) {
|
File file = new File(filePath + fileName);
|
File file1 = new File(filePath);
|
file.delete();
|
file1.delete();
|
}
|
|
/**
|
* 合并pdf
|
*
|
* @param files
|
* @param newfile
|
* @return
|
*/
|
public static boolean mergePdfFiles(String[] files, String newfile) {
|
boolean retValue = false;
|
Document document = null;
|
try {
|
document = new Document(new PdfReader(files[0]).getPageSize(1));
|
PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile));
|
document.open();
|
for (int i = 0; i < files.length; i++) {
|
PdfReader reader = new PdfReader(files[i]);
|
int n = reader.getNumberOfPages();
|
for (int j = 1; j <= n; j++) {
|
document.newPage();
|
PdfImportedPage page = copy.getImportedPage(reader, j);
|
copy.addPage(page);
|
}
|
}
|
retValue = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new GlobalException("合并数据失败!");
|
} finally {
|
document.close();
|
}
|
return retValue;
|
}
|
|
/**
|
* 使用pdfbox将整个pdf转换成图片
|
*
|
* @param pdfPath 文件地址 如:C:\\Users\\user\\Desktop\\test
|
* @param imagePath PDF文件名不带后缀名
|
*/
|
public static void pdf2png(String pdfPath, String imagePath) {
|
long startTime = System.currentTimeMillis();
|
// 将文件地址和文件名拼接成路径 注意:线上环境不能使用\\拼接
|
File file = new File(pdfPath);
|
try {
|
// 写入文件
|
PDDocument doc = PDDocument.load(file);
|
PDFRenderer renderer = new PDFRenderer(doc);
|
int pageCount = doc.getNumberOfPages();
|
for (int i = 0; i < pageCount; i++) {
|
// dpi为144,越高越清晰,转换越慢
|
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
|
// 将图片写出到该路径下
|
ImageIO.write(image, "png", new File(imagePath));
|
}
|
long endTime = System.currentTimeMillis();
|
System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒"); //转化用时
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|