
1 changed files with 168 additions and 0 deletions
@ -0,0 +1,168 @@ |
|||
package com.yxt.common.base.config.component; |
|||
|
|||
import com.jacob.activeX.ActiveXComponent; |
|||
import com.jacob.com.Dispatch; |
|||
import com.yxt.common.base.utils.DateUtils; |
|||
import freemarker.template.Configuration; |
|||
import freemarker.template.Template; |
|||
import freemarker.template.Version; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
import sun.misc.BASE64Encoder; |
|||
|
|||
import java.io.*; |
|||
import java.util.Date; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author liuguohui |
|||
* @version 1.0 |
|||
* @description 生成word及word转pdf 组件 |
|||
* @date 2022/04/09 |
|||
*/ |
|||
@Component |
|||
public class DocPdfComponent { |
|||
|
|||
private static final Logger log = LoggerFactory.getLogger(DocPdfComponent.class); |
|||
|
|||
@Value("${templateUrl.uploadUrl:static/template/}") |
|||
private String uploadTemplateUrl; |
|||
|
|||
@Value("${templateUrl.prefixUrl:http://127.0.0.1:8080/template/}") |
|||
private String prefixTemplateUrl; |
|||
|
|||
/** |
|||
* 根据ftl模板生成word |
|||
* |
|||
* @param map 数据 |
|||
* @param typeName 模板名称 |
|||
* @param fileName 文件名 |
|||
* @return 返回word文件路径和链接路径 |
|||
*/ |
|||
public Map<String, String> creatWord(Map<String, Object> map, String typeName, String fileName) { |
|||
String dateStr = DateUtils.dateConvertStr(new Date(), "yyyyMMdd"); |
|||
String targetPath = uploadTemplateUrl + dateStr; |
|||
try { |
|||
//Configuration 用于读取ftl文件
|
|||
Configuration configuration = new Configuration(new Version("2.3.0")); |
|||
configuration.setDefaultEncoding("utf-8"); |
|||
//指定路径的第一种方式(根据某个类的相对路径指定)
|
|||
// configuration.setClassForTemplateLoading(this.getClass(), "");
|
|||
//指定路径的第二种方式
|
|||
configuration.setDirectoryForTemplateLoading(new File(uploadTemplateUrl)); |
|||
//输出文档路径及名称
|
|||
File targetFile = new File(targetPath); |
|||
if (!targetFile.exists()) { |
|||
targetFile.mkdirs(); |
|||
} |
|||
targetPath = targetPath + File.separator + fileName; |
|||
File outFile = new File(targetPath); |
|||
//以utf-8的编码读取ftl文件
|
|||
Template template = configuration.getTemplate(typeName + ".ftl", "utf-8"); |
|||
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240); |
|||
template.process(map, out); |
|||
out.close(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
Map<String, String> resultMap = new HashMap<>(); |
|||
String prefixUrl = prefixTemplateUrl + targetPath.replace(uploadTemplateUrl, ""); |
|||
resultMap.put("uploadTemplateUrl", targetPath); // 文件路径
|
|||
resultMap.put("prefixTemplateUrl", prefixUrl); // 链接路径
|
|||
return resultMap; |
|||
} |
|||
|
|||
/** |
|||
* 图片文件转换 |
|||
* @param imgPath |
|||
* @return |
|||
*/ |
|||
public String getImageStr(String imgPath) { |
|||
String imgFile = imgPath; |
|||
InputStream in = null; |
|||
byte[] data = null; |
|||
try { |
|||
in = new FileInputStream(imgFile); |
|||
data = new byte[in.available()]; |
|||
in.read(data); |
|||
in.close(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
BASE64Encoder encoder = new BASE64Encoder(); |
|||
return encoder.encode(data); |
|||
} |
|||
|
|||
/** |
|||
* word转换为pdf |
|||
* @param wordFile word的路径 |
|||
* @param pdfName pdf名称 |
|||
* @return 返回pdf文件路径和链接路径 |
|||
*/ |
|||
public Map<String, String> doc2pdf(String wordFile, String pdfName) { |
|||
String dateStr = DateUtils.dateConvertStr(new Date(), "yyyyMMdd"); |
|||
String targetPath = uploadTemplateUrl + dateStr; |
|||
ActiveXComponent app = null; |
|||
Map<String, String> resultMap = new HashMap<>(); |
|||
log.info("doc开始转换pdf..."); |
|||
try { |
|||
// 打开word
|
|||
app = new ActiveXComponent("Word.Application"); |
|||
// 获得word中所有打开的文档
|
|||
Dispatch documents = app.getProperty("Documents").toDispatch(); |
|||
// 打开文档
|
|||
Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); |
|||
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
|||
File targetFile = new File(targetPath); |
|||
if (!targetFile.exists()) { |
|||
targetFile.mkdirs(); |
|||
} |
|||
String pdfFile = targetPath + File.separator + pdfName; |
|||
File target = new File(pdfFile); |
|||
if (target.exists()) { |
|||
target.delete(); |
|||
} |
|||
Dispatch.call(document, "SaveAs", pdfFile, 17); |
|||
// 关闭文档
|
|||
Dispatch.call(document, "Close", false); |
|||
String prefixUrl = prefixTemplateUrl + pdfFile.replace(uploadTemplateUrl, ""); |
|||
resultMap.put("uploadTemplateUrl", pdfFile); // 文件路径
|
|||
resultMap.put("prefixTemplateUrl", prefixUrl); // 链接路径
|
|||
} catch (Exception e) { |
|||
log.error("转换失败" + e.getMessage()); |
|||
} finally { |
|||
// 关闭office
|
|||
app.invoke("Quit", 0); |
|||
} |
|||
return resultMap; |
|||
} |
|||
|
|||
/** |
|||
* 根据ftl模板生成word并转为pdf |
|||
* @param map |
|||
* @param typeName |
|||
* @param docName |
|||
* @return 返回pdf文件路径和链接路径 |
|||
*/ |
|||
public Map<String, String> createDocToPdf(Map<String, Object> map, String typeName, String docName) { |
|||
// 生成doc文件
|
|||
Map<String, String> docMap = creatWord(map, typeName, docName); |
|||
String uploadTemplateUrl = docMap.get("uploadTemplateUrl"); |
|||
// pdf的文件名
|
|||
String pdfName = docName.replace(".doc", ".pdf"); |
|||
// 生成pdf文件
|
|||
Map<String, String> pdfMap = doc2pdf(uploadTemplateUrl, pdfName); |
|||
return pdfMap; |
|||
} |
|||
|
|||
public String getUploadTemplateUrl() { |
|||
return uploadTemplateUrl; |
|||
} |
|||
|
|||
public String getPrefixTemplateUrl() { |
|||
return prefixTemplateUrl; |
|||
} |
|||
} |
Loading…
Reference in new issue