parent
a5547a550d
commit
9e42d8ba45
5 changed files with 303 additions and 19 deletions
@ -0,0 +1,189 @@ |
||||
package org.jeecg.modules.demo.utils; |
||||
|
||||
import cn.afterturn.easypoi.word.WordExportUtil; |
||||
import com.aliyuncs.utils.IOUtils; |
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
||||
import org.docx4j.Docx4J; |
||||
import org.docx4j.convert.out.FOSettings; |
||||
import org.docx4j.fonts.IdentityPlusMapper; |
||||
import org.docx4j.fonts.Mapper; |
||||
import org.docx4j.fonts.PhysicalFonts; |
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.io.*; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @description: |
||||
* @param: |
||||
* @return: |
||||
* @author: z.h.c |
||||
* @date: 24/11/22 10:17 |
||||
*/ |
||||
public class FileUtils { |
||||
/** |
||||
* |
||||
* @param templatePath |
||||
* @param saveDir |
||||
* @param fileName |
||||
* @param params |
||||
* @return |
||||
*/ |
||||
public static String exportWord(String templatePath, String saveDir, String fileName, Map<String, Object> params) { |
||||
Assert.notNull(templatePath, "模板路径不能为空"); |
||||
Assert.notNull(saveDir, "临时文件路径不能为空"); |
||||
Assert.notNull(fileName, "导出文件名不能为空"); |
||||
Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式"); |
||||
if (!saveDir.endsWith("/")) { |
||||
saveDir = saveDir + File.separator; |
||||
} |
||||
|
||||
File dir = new File(saveDir); |
||||
if (!dir.exists()) { |
||||
dir.mkdirs(); |
||||
} |
||||
String savePath = saveDir + fileName; |
||||
|
||||
try { |
||||
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params); |
||||
FileOutputStream fos = new FileOutputStream(savePath); |
||||
doc.write(fos); |
||||
fos.flush(); |
||||
fos.close(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return savePath; |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* @param wordPath word文件路径 |
||||
* @param pdfPath pdf输出路径 |
||||
* @return |
||||
*/ |
||||
public static String convertDocx2Pdf(String wordPath, String pdfPath) { |
||||
OutputStream os = null; |
||||
InputStream is = null; |
||||
if (pdfPath.endsWith("/")) { |
||||
pdfPath = pdfPath + File.separator; |
||||
} |
||||
try { |
||||
is = new FileInputStream(new File(wordPath)); |
||||
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(is); |
||||
Mapper fontMapper = new IdentityPlusMapper(); |
||||
fontMapper.put("隶书", PhysicalFonts.get("LiSu")); |
||||
fontMapper.put("宋体", PhysicalFonts.get("SimSun")); |
||||
fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei")); |
||||
fontMapper.put("黑体", PhysicalFonts.get("SimHei")); |
||||
fontMapper.put("楷体", PhysicalFonts.get("KaiTi")); |
||||
fontMapper.put("新宋体", PhysicalFonts.get("NSimSun")); |
||||
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai")); |
||||
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong")); |
||||
fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB")); |
||||
fontMapper.put("仿宋", PhysicalFonts.get("FangSong")); |
||||
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312")); |
||||
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan")); |
||||
fontMapper.put("华文宋体", PhysicalFonts.get("STSong")); |
||||
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong")); |
||||
mlPackage.setFontMapper(fontMapper); |
||||
|
||||
os = new FileOutputStream(pdfPath); |
||||
|
||||
//docx4j docx转pdf
|
||||
FOSettings foSettings = Docx4J.createFOSettings(); |
||||
foSettings.setWmlPackage(mlPackage); |
||||
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); |
||||
|
||||
is.close();//关闭输入流
|
||||
os.close();//关闭输出流
|
||||
|
||||
return ""; |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
try { |
||||
if (is != null) { |
||||
is.close(); |
||||
} |
||||
if (os != null) { |
||||
os.close(); |
||||
} |
||||
} catch (Exception ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
} finally { |
||||
File file = new File(wordPath); |
||||
if (file != null && file.isFile() && file.exists()) { |
||||
file.delete(); |
||||
} |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
/** |
||||
* docx文档转换为PDF |
||||
* |
||||
* @param pdfPath PDF文档存储路径 |
||||
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等 |
||||
*/ |
||||
public static void convertDocxToPdf(String docxPath, String pdfPath) throws Exception { |
||||
|
||||
FileOutputStream fileOutputStream = null; |
||||
try { |
||||
File file = new File(docxPath); |
||||
fileOutputStream = new FileOutputStream(new File(pdfPath)); |
||||
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file); |
||||
setFontMapper(mlPackage); |
||||
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath))); |
||||
}catch (Exception e){ |
||||
e.printStackTrace(); |
||||
// log.error("docx文档转换为PDF失败");
|
||||
}finally { |
||||
IOUtils.closeQuietly(fileOutputStream); |
||||
} |
||||
} |
||||
|
||||
private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception { |
||||
Mapper fontMapper = new IdentityPlusMapper(); |
||||
fontMapper.put("隶书", PhysicalFonts.get("LiSu")); |
||||
fontMapper.put("宋体", PhysicalFonts.get("SimSun")); |
||||
fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei")); |
||||
fontMapper.put("黑体", PhysicalFonts.get("SimHei")); |
||||
fontMapper.put("楷体", PhysicalFonts.get("KaiTi")); |
||||
fontMapper.put("新宋体", PhysicalFonts.get("NSimSun")); |
||||
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai")); |
||||
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong")); |
||||
fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB")); |
||||
fontMapper.put("仿宋", PhysicalFonts.get("FangSong")); |
||||
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312")); |
||||
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan")); |
||||
fontMapper.put("华文宋体", PhysicalFonts.get("STSong")); |
||||
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong")); |
||||
|
||||
mlPackage.setFontMapper(fontMapper); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @param path 图片路径 |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static byte[] getImageBase64(String path) throws IOException { |
||||
InputStream input = new FileInputStream(path); |
||||
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
||||
byte[] buf = new byte[1024]; |
||||
int numBytesRead = 0; |
||||
while ((numBytesRead = input.read(buf)) != -1) { |
||||
output.write(buf, 0, numBytesRead); |
||||
} |
||||
byte[] data = output.toByteArray(); |
||||
output.close(); |
||||
input.close(); |
||||
return data; |
||||
} |
||||
|
||||
} |
||||
|
Loading…
Reference in new issue