提交 ffb622c7 编写于 作者: O o2null

Merge branch 'feature/增加html转图片接口工具' into 'develop_java8'

[流程平台]增加html转图片接口工具

See merge request o2oa/o2oa!3144

(cherry picked from commit 195ef8b1)

97c16a78 增加html转图片接口工具
29170535 增加html转图片接口工具第三方jar
上级 6382d5ff
......@@ -350,6 +350,10 @@
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
</dependency>
<dependency>
<groupId>com.github.xuwei-k</groupId>
<artifactId>html2image</artifactId>
</dependency>
</dependencies>
<build>
......@@ -876,6 +880,11 @@
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.github.xuwei-k</groupId>
<artifactId>html2image</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>o2oa</groupId>
<artifactId>x_base_core_project</artifactId>
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import com.google.gson.JsonElement;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.StorageMapping;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.FileTools;
import com.x.general.core.entity.GeneralFile;
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.assemble.surface.ThisApplication;
import com.x.processplatform.assemble.surface.WorkControl;
import com.x.processplatform.core.entity.content.Attachment;
import com.x.processplatform.core.entity.content.Work;
import gui.ava.html.image.generator.HtmlImageGenerator;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;
import javax.print.DocFlavor;
import java.io.File;
import java.util.Date;
class ActionHtmlToImage extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionEdit.class);
ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
ActionResult<Wo> result = new ActionResult<>();
Business business = new Business(emc);
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
Wo wo = new Wo();
String id = saveImage(wi, effectivePerson, business);
wo.setId(id);
result.setData(wo);
return result;
}
}
public static class Wo extends WoId {
}
private String saveImage(Wi wi, EffectivePerson effectivePerson, Business business) throws Exception{
Work work = null;
EntityManagerContainer emc = business.entityManagerContainer();
if(StringUtils.isNotBlank(wi.getWorkId())) {
/* 后面要重新保存 */
work = emc.find(wi.getWorkId(), Work.class);
/* 判断work是否存在 */
if (null == work) {
throw new ExceptionEntityNotExist(wi.getWorkId(), Work.class);
}
WoControl control = business.getControl(effectivePerson, work, WoControl.class);
if (BooleanUtils.isNotTrue(control.getAllowSave())) {
throw new ExceptionAccessDenied(effectivePerson, work);
}
}
String workHtml = wi.getWorkHtml();
if (StringUtils.isEmpty(workHtml)) {
workHtml = "无内容";
}
if (workHtml.toLowerCase().indexOf("<html") == -1) {
workHtml = "<html><head></head><body>" + workHtml + "</body></html>";
}
String name = StringUtils.split(effectivePerson.getDistinguishedName(),"@")[0] + DateTools.compact(new Date()) + ".png";
if (StringUtils.isNotEmpty(wi.getTitle())) {
name = wi.getTitle() + ".png";
}
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
imageGenerator.loadHtml(workHtml);
File tempDir = Config.dir_local_temp();
FileTools.forceMkdir(tempDir);
File file = new File(tempDir, name);
imageGenerator.saveAsImage(file);
byte[] bytes = FileUtils.readFileToByteArray(file);
String key = "";
if(work!=null){
StorageMapping mapping = ThisApplication.context().storageMappings().random(Attachment.class);
Attachment attachment = this.concreteAttachment(work, effectivePerson, wi.getSite());
attachment.saveContent(mapping, bytes, name);
attachment.setType((new Tika()).detect(bytes, name));
emc.beginTransaction(Attachment.class);
emc.persist(attachment, CheckPersistType.all);
emc.commit();
key = attachment.getId();
}else{
StorageMapping gfMapping = ThisApplication.context().storageMappings().random(GeneralFile.class);
GeneralFile generalFile = new GeneralFile(gfMapping.getName(), name, effectivePerson.getDistinguishedName());
generalFile.saveContent(gfMapping, bytes, name);
emc.beginTransaction(GeneralFile.class);
emc.persist(generalFile, CheckPersistType.all);
emc.commit();
key = generalFile.getId();
}
return key;
}
private Attachment concreteAttachment(Work work, EffectivePerson effectivePerson, String site) throws Exception {
Attachment attachment = new Attachment();
attachment.setCompleted(false);
attachment.setPerson(effectivePerson.getDistinguishedName());
attachment.setLastUpdatePerson(effectivePerson.getDistinguishedName());
attachment.setSite(site);
/** 用于判断目录的值 */
attachment.setWorkCreateTime(work.getCreateTime());
attachment.setApplication(work.getApplication());
attachment.setProcess(work.getProcess());
attachment.setJob(work.getJob());
attachment.setActivity(work.getActivity());
attachment.setActivityName(work.getActivityName());
attachment.setActivityToken(work.getActivityToken());
attachment.setActivityType(work.getActivityType());
return attachment;
}
public static class Wi extends GsonPropertyObject {
@FieldDescribe("*待转换html.")
private String workHtml;
@FieldDescribe("图片标题")
private String title;
@FieldDescribe("工作标识,把图片保存到工单的附件中,非必填")
private String workId;
@FieldDescribe("位置,工作标识不为空的时候必填")
private String site;
public String getWorkHtml() {
return workHtml;
}
public void setWorkHtml(String workHtml) {
this.workHtml = workHtml;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWorkId() {
return workId;
}
public void setWorkId(String workId) {
this.workId = workId;
}
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
}
public static class WoControl extends WorkControl {
}
}
......@@ -1215,4 +1215,22 @@ public class AttachmentAction extends StandardJaxrsAction {
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
@JaxrsMethodDescribe(value = "html转图片工具类,转换后如果工作不为空通过downloadWithWork接口下载,为空downloadTransfer接口下载.", action = ActionHtmlToImage.class)
@POST
@Path("html/to/image")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.APPLICATION_JSON)
public void htmlToImage(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
JsonElement jsonElement) {
ActionResult<ActionHtmlToImage.Wo> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionHtmlToImage().execute(effectivePerson, jsonElement);
} catch (Exception e) {
logger.error(e, effectivePerson, request, null);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册