From d98b392093d16159496e0638c21f6e4861ec2048 Mon Sep 17 00:00:00 2001 From: o2sword <171715986@qq.com> Date: Wed, 1 Jul 2020 13:15:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96html=E8=BD=ACpdf=E4=B8=BA?= =?UTF-8?q?=E9=80=9A=E7=94=A8=E5=B7=A5=E5=85=B7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../factory/element/ProcessFactory.java | 27 +--- .../attachment/ActionDownloadTransfer.java | 39 ++++++ .../jaxrs/attachment/ActionHtmlToPdf.java | 124 ++++++++++++++++++ .../attachment/ActionUploadWorkInfo.java | 3 +- .../jaxrs/attachment/AttachmentAction.java | 38 +++++- 5 files changed, 203 insertions(+), 28 deletions(-) create mode 100644 o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionDownloadTransfer.java create mode 100644 o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToPdf.java diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/factory/element/ProcessFactory.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/factory/element/ProcessFactory.java index 812ce53798..c3ae4e6246 100644 --- a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/factory/element/ProcessFactory.java +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/factory/element/ProcessFactory.java @@ -213,11 +213,10 @@ public class ProcessFactory extends ElementFactory { public List listControlableProcess(EffectivePerson effectivePerson, Application application) throws Exception { - Business business = this.business(); List ids = this.listWithApplication(application); List list = new ArrayList<>(); for (String str : ids) { - Process o = business.process().pick(str); + Process o = this.pick(str); if (null != o) { if (effectivePerson.isPerson(o.getControllerList())) { list.add(str); @@ -259,30 +258,6 @@ public class ProcessFactory extends ElementFactory { return null; } - // /* 判断用户是否有管理权限 */ - // public boolean allowControl(EffectivePerson effectivePerson, Process process) - // throws Exception { - // if (effectivePerson.isManager()) { - // return true; - // } - // if (null != process) { - // if (effectivePerson.isUser(process.getControllerList())) { - // return true; - // } - // Application application = - // this.business().application().pick(process.getApplication()); - // if (null != application) { - // if (effectivePerson.isUser(application.getControllerList())) { - // return true; - // } - // if (effectivePerson.isUser(application.getCreatorPerson())) { - // return true; - // } - // } - // } - // return false; - // } - public List sort(List list) { list = list.stream() .sorted(Comparator.comparing(Process::getAlias, Comparator.nullsLast(String::compareTo)) diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionDownloadTransfer.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionDownloadTransfer.java new file mode 100644 index 0000000000..e572a9d30c --- /dev/null +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionDownloadTransfer.java @@ -0,0 +1,39 @@ +package com.x.processplatform.assemble.surface.jaxrs.attachment; + +import com.x.base.core.container.EntityManagerContainer; +import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.project.http.ActionResult; +import com.x.base.core.project.http.EffectivePerson; +import com.x.base.core.project.jaxrs.WoFile; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import net.sf.ehcache.Element; + +class ActionDownloadTransfer extends BaseAction { + + private static Logger logger = LoggerFactory.getLogger(ActionDownloadTransfer.class); + + ActionResult execute(EffectivePerson effectivePerson, String flag) throws Exception { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + ActionResult result = new ActionResult<>(); + Wo wo = null; + Element element = cache.get(flag); + if ((null != element) && (null != element.getObjectValue())) { + CacheResultObject ro = (CacheResultObject) element.getObjectValue(); + wo = new Wo(ro.getBytes(), this.contentType(false, ro.getName()), + this.contentDisposition(false, ro.getName())); + } + + result.setData(wo); + return result; + } + } + + public static class Wo extends WoFile { + + public Wo(byte[] bytes, String contentType, String contentDisposition) { + super(bytes, contentType, contentDisposition); + } + + } +} diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToPdf.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToPdf.java new file mode 100644 index 0000000000..7ff318e18b --- /dev/null +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToPdf.java @@ -0,0 +1,124 @@ +package com.x.processplatform.assemble.surface.jaxrs.attachment; + +import com.google.gson.JsonElement; +import com.itextpdf.html2pdf.ConverterProperties; +import com.itextpdf.html2pdf.HtmlConverter; +import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider; +import com.itextpdf.kernel.geom.PageSize; +import com.itextpdf.kernel.pdf.PdfDocument; +import com.itextpdf.kernel.pdf.PdfWriter; +import com.x.base.core.container.EntityManagerContainer; +import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.project.annotation.FieldDescribe; +import com.x.base.core.project.cache.ApplicationCache; +import com.x.base.core.project.config.Config; +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 net.sf.ehcache.Element; +import org.apache.commons.lang3.StringUtils; + +import java.io.*; +import java.util.UUID; + +class ActionHtmlToPdf extends BaseAction { + + private static Logger logger = LoggerFactory.getLogger(ActionEdit.class); + + ActionResult execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + ActionResult result = new ActionResult<>(); + Wi wi = this.convertToWrapIn(jsonElement, Wi.class); + Wo wo = new Wo(); + String id = savePdf(wi, effectivePerson.getDistinguishedName()); + wo.setId(id); + result.setData(wo); + return result; + } + } + + public static class Wo extends WoId { + + } + + private String savePdf(Wi wi, String person){ + try { + CacheResultObject ro = new CacheResultObject(); + ro.setPerson(person); + + String workHtml = wi.getWorkHtml(); + if(StringUtils.isEmpty(workHtml)){ + workHtml = "无内容"; + } + if(workHtml.toLowerCase().indexOf(""; + } + String title = person + DateTools.now()+".pdf"; + if(StringUtils.isNotEmpty(wi.getTitle())){ + title = wi.getTitle()+".pdf"; + } + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + ConverterProperties props = new ConverterProperties(); + DefaultFontProvider dfp = new DefaultFontProvider(false, false, false); + //dfp.addFont(Config.base()+"/commons/fonts/NotoSansCJKsc-Regular.otf"); + dfp.addDirectory(Config.base()+"/commons/fonts"); + props.setFontProvider(dfp); + PdfWriter writer = new PdfWriter(out); + PdfDocument pdf = new PdfDocument(writer); + float width = PageSize.A4.getWidth(); + if(wi.getPageWidth()!=null && wi.getPageWidth()>100){ + width = wi.getPageWidth().floatValue(); + } + pdf.setDefaultPageSize(new PageSize(width, PageSize.A4.getHeight())); + HtmlConverter.convertToPdf(workHtml, pdf, props); + ro.setBytes(out.toByteArray()); + ro.setName(title + ".pdf"); + } + + String cacheKey = ApplicationCache.concreteCacheKey(UUID.randomUUID().toString()); + cache.put(new Element(cacheKey,ro)); + return cacheKey; + } catch (Exception e) { + logger.warn("写work信息异常"+e.getMessage()); + } + return ""; + } + + public static class Wi extends GsonPropertyObject { + + @FieldDescribe("待转换html.") + private String workHtml; + + @FieldDescribe("转pdf页面宽度,默认A4.") + private Float pageWidth; + + @FieldDescribe("pdf标题") + private String title; + + public String getWorkHtml() { return workHtml; } + + public void setWorkHtml(String workHtml) { + this.workHtml = workHtml; + } + + public Float getPageWidth() { + return pageWidth; + } + + public void setPageWidth(Float pageWidth) { + this.pageWidth = pageWidth; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + } +} diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWorkInfo.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWorkInfo.java index 05960de392..e55b8d9a6e 100644 --- a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWorkInfo.java +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWorkInfo.java @@ -88,7 +88,8 @@ class ActionUploadWorkInfo extends BaseAction { try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { ConverterProperties props = new ConverterProperties(); DefaultFontProvider dfp = new DefaultFontProvider(false, false, false); - dfp.addFont(Config.base()+"/commons/fonts/NotoSansCJKsc-Regular.otf"); + //dfp.addFont(Config.base()+"/commons/fonts/NotoSansCJKsc-Regular.otf"); + dfp.addDirectory(Config.base()+"/commons/fonts"); props.setFontProvider(dfp); PdfWriter writer = new PdfWriter(out); PdfDocument pdf = new PdfDocument(writer); diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java index 05a62fd417..e5f6149e9a 100644 --- a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java @@ -840,7 +840,7 @@ public class AttachmentAction extends StandardJaxrsAction { @JaxrsMethodDescribe(value = "管理员上传附件.", action = ActionManageUpload.class) @POST - @Path("upload/work/{workId}U/manage") + @Path("upload/work/{workId}/manage") @Produces(HttpMediaType.APPLICATION_JSON_UTF_8) @Consumes(MediaType.MULTIPART_FORM_DATA) public void manageUpload(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, @@ -862,4 +862,40 @@ public class AttachmentAction extends StandardJaxrsAction { } asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); } + + @JaxrsMethodDescribe(value = "html转pdf工具类,转换后通过downloadTransfer接口下载", action = ActionHtmlToPdf.class) + @POST + @Path("html/to/pdf") + @Produces(HttpMediaType.APPLICATION_JSON_UTF_8) + @Consumes(MediaType.APPLICATION_JSON) + public void htmlToPdf(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, + JsonElement jsonElement) { + ActionResult result = new ActionResult<>(); + EffectivePerson effectivePerson = this.effectivePerson(request); + try { + result = new ActionHtmlToPdf().execute(effectivePerson, jsonElement); + } catch (Exception e) { + logger.error(e, effectivePerson, request, null); + result.error(e); + } + asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); + } + + @JaxrsMethodDescribe(value = "下载转换后的附件", action = ActionDownloadTransfer.class) + @GET + @Path("download/transfer/flag/{flag}") + @Consumes(MediaType.APPLICATION_JSON) + public void downloadTransfer(@Suspended final AsyncResponse asyncResponse, + @Context HttpServletRequest request, + @JaxrsParameterDescribe("*转换后附件id") @PathParam("flag") String flag){ + ActionResult result = new ActionResult<>(); + EffectivePerson effectivePerson = this.effectivePerson(request); + try { + result = new ActionDownloadTransfer().execute(effectivePerson, flag); + } catch (Exception e) { + logger.error(e, effectivePerson, request, null); + result.error(e); + } + asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); + } } \ No newline at end of file -- GitLab