1、Wf2PdfUtil
此工具类为将流程表单转为pdf的工具类代码,可以根据实际传入参数即可实现将表单转为pdf或html,用于和档案系统集成时较多。
package com.weavernorth;
import com.engine.common.util.ServiceUtil;
import com.engine.workflow.service.HtmlToPdfService;
import com.engine.workflow.service.impl.HtmlToPdfServiceImpl;
import net.sf.json.JSONObject;
import weaver.file.FileSecurityUtil;
import weaver.file.FileUpload;
import weaver.file.ImageFileManager;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.UserManager;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.system.SystemComInfo;
import weaver.workflow.workflow.WorkflowConfigComInfo;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @Classname Wf2PdfUtil
* @Description TODO
* @Version 1.0.0
* @Date 2023/5/10 8:51
* @Created by 渣渣
*/
public class Wf2PdfUtil {
private Logger log = LoggerFactory.getLogger(Wf2PdfUtil.class);
/**
* @param requestid
* @param isKeepsign 是否需要签字意见
* @param onlyHtml 0 只转pdf 1只转html 2 都转
* @return
*/
public Map getwfPDF(String requestid, boolean isKeepsign, String onlyHtml) {
String keepsign = "0";
if (isKeepsign) {
keepsign = "1";
}
UserManager userManager = new UserManager();
User user = userManager.getUserByUserIdAndLoginType(1, "1");
HashMap hashMap = new HashMap();
hashMap.put("requestid", requestid);
hashMap.put("limitauth", "0");
//0 只转pdf 1只转html 2 都转
hashMap.put("onlyHtml", onlyHtml);
hashMap.put("keepsign", keepsign);
Map filemap = null;
try {
log.info("表单转PDF参数:" + JSONObject.fromObject(hashMap).toString());
filemap = this.getFileId(hashMap, user);
log.info("表单转PDF结果:" + JSONObject.fromObject(filemap).toString());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return filemap;
}
public String getFileSavePath() {
SystemComInfo systemComInfo = new SystemComInfo();
String createDir = FileUpload.getCreateDir(systemComInfo.getFilesystem());
createDir = createDir.replace("\\", "/");
if (createDir.endsWith("/")) {
createDir = createDir.substring(0, createDir.length() - 1);
}
return createDir;
}
public Map<String, String> getFileId(Map<String, Object> hashMap, User user) {
HashMap returnMap = new HashMap();
hashMap.put("path", this.getFileSavePath());
hashMap.put("isTest", "1");
this.injectuseWk(hashMap);
HtmlToPdfService htmlToPdfService = (HtmlToPdfService) ServiceUtil.getService(HtmlToPdfServiceImpl.class, user);
Object o = htmlToPdfService != null ? htmlToPdfService.getFormDatas(hashMap) : new HashMap();
String path = Util.null2String(((Map) o).get("path"));
String onlyHtml = Util.null2String(hashMap.get("onlyHtml"));
String filename;
String filePath;
String fileid;
//只转为pdf
if (onlyHtml.equals("0")) {
filename = Util.null2String(((Map) o).get("filename"));
filePath = path + File.separator + filename;
fileid = this.saveAsFile(filePath, filename);
if (!"".equals(fileid)) {
returnMap.put("pdf", fileid);
}
}
//转为html
else if (onlyHtml.equals("1")) {
filename = Util.null2String(((Map) o).get("filename"));
filePath = path + File.separator + filename;
fileid = this.saveAsFile(filePath, filename);
if (!"".equals(fileid)) {
returnMap.put("html", fileid);
}
}
//既要转pdf又要转html
else if (onlyHtml.equals("2")) {
filename = Util.null2String(((Map) o).get("pdffilename"));
filePath = Util.null2String(((Map) o).get("htmlfilename"));
fileid = path + File.separator + filename;
String htmlFullPath = path + File.separator + filePath;
String pdf_id = this.saveAsFile(fileid, filename);
String htmlfileid = this.saveAsFile(htmlFullPath, filePath);
if (!"".equals(pdf_id)) {
returnMap.put("pdf", pdf_id);
}
if (!"".equals(htmlfileid)) {
returnMap.put("html", htmlfileid);
}
}
return returnMap;
}
public String saveAsFile(String filepath, String filename) {
FileInputStream fileInputStream = null;
byte[] bytes = null;
File file = new File(filepath);
if (file.exists()) {
try {
fileInputStream = new FileInputStream(file);
bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
ImageFileManager imageFileManager = new ImageFileManager();
imageFileManager.setComefrom("WorkflowToDoc");
imageFileManager.setData(bytes);
imageFileManager.setImagFileName(filename);
int imageFileid = imageFileManager.saveImageFile();
if (imageFileid <= 0) {
log.info("保存离线HTML/PDF文件失败");
return "";
} else {
FileSecurityUtil.deleteFile(file);
return imageFileid + "";
}
}
private void injectuseWk(Map<String, Object> map) {
if (!map.containsKey("useWk")) {
WorkflowConfigComInfo workflowConfigComInfo = new WorkflowConfigComInfo();
String usewk = workflowConfigComInfo.getValue("htmltopdf_usewk");
map.put("useWk", usewk);
}
}
}
2、测试方法
Wf2PdfUtil wf2PdfUtil = new Wf2PdfUtil();
Map map = wf2PdfUtil.getwfPDF("236236", true, "2");
//map的值={pdf=3251, html=3252}
核对文件,查询数据库
表单存为pdf
html文件
3、引入字体
需要在对应目录下引入字体文件,否则可能导致转出的pdf样式缺失。
如下图怪怪的。
引入字体到对应目录:/weaver/ecology/font/workflow/pdf