Просмотр исходного кода

Merge branch 'fix/提取html转pdf为通用工具接口' into 'develop'

[平台工具]新增提取html转pdf为通用工具接口

See merge request o2oa/o2oa!961
李义 5 лет назад
Родитель
Сommit
4361aabed0

+ 1 - 26
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<String> listControlableProcess(EffectivePerson effectivePerson, Application application)
 			throws Exception {
-		Business business = this.business();
 		List<String> ids = this.listWithApplication(application);
 		List<String> 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 <T extends Process> List<T> sort(List<T> list) {
 		list = list.stream()
 				.sorted(Comparator.comparing(Process::getAlias, Comparator.nullsLast(String::compareTo))

+ 39 - 0
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<Wo> execute(EffectivePerson effectivePerson, String flag) throws Exception {
+		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
+			ActionResult<Wo> 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);
+		}
+
+	}
+}

+ 124 - 0
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<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
+		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
+			ActionResult<Wo> 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("<html") == -1){
+				workHtml = "<html><head></head><body>" + workHtml + "</body></html>";
+			}
+			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;
+		}
+	}
+}

+ 2 - 1
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);

+ 37 - 1
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<ActionHtmlToPdf.Wo> 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<ActionDownloadTransfer.Wo> 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));
+	}
 }