diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java index a9c93c31952f7bdc483861576d2ef0d4d0edaa79..f929ac6ca35c3c72a7d59fd3258409a78c665514 100644 --- a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java @@ -78,11 +78,12 @@ class ActionHtmlToImage extends BaseAction { workHtml = "无内容"; } if (workHtml.toLowerCase().indexOf(""; + workHtml = "" + + "" + workHtml + ""; } - String name = StringUtils.split(effectivePerson.getDistinguishedName(),"@")[0] + DateTools.compact(new Date()) + ".jpg"; + String name = StringUtils.split(effectivePerson.getDistinguishedName(),"@")[0] + DateTools.compact(new Date()) + ".png"; if (StringUtils.isNotEmpty(wi.getTitle())) { - name = wi.getTitle() + ".jpg"; + name = wi.getTitle() + ".png"; } byte[] bytes = null; try (Playwright playwright = Playwright.create()) { @@ -102,16 +103,16 @@ class ActionHtmlToImage extends BaseAction { page.setContent(workHtml); Page.ScreenshotOptions screenshotOptions = new Page.ScreenshotOptions(); screenshotOptions.setFullPage(true); + if (wi.getHtmlWidth() != null && wi.getHtmlHeight() != null) { + screenshotOptions.setClip(wi.getStartX(), wi.getStartY(), wi.getHtmlWidth(), wi.getHtmlHeight()); + } + if(BooleanUtils.isTrue(wi.getOmitBackground())){ + screenshotOptions.setOmitBackground(wi.getOmitBackground()); + } File tempDir = Config.dir_local_temp(); FileTools.forceMkdir(tempDir); File file = new File(tempDir, name); - //screenshotOptions.setPath(Paths.get("/Users/chengjian/dev/tmp/screenshot-" + browserType.name() + ".png")); screenshotOptions.setPath(file.toPath()); - if(wi.getQuality()!=null && wi.getQuality()>20){ - screenshotOptions.setQuality(wi.getQuality()); - }else { - screenshotOptions.setQuality(80); - } page.screenshot(screenshotOptions); bytes = FileUtils.readFileToByteArray(file); break; @@ -173,8 +174,16 @@ class ActionHtmlToImage extends BaseAction { private String workHtml; @FieldDescribe("图片标题") private String title; - @FieldDescribe("图片质量,默认80,值越大越清晰") - private Integer quality; + @FieldDescribe("html正文宽度,允许为空.") + private Double htmlWidth; + @FieldDescribe("html正文高度,允许为空.") + private Double htmlHeight; + @FieldDescribe("html的X轴开始位置,允许为空.") + private Double startX; + @FieldDescribe("html的Y轴开始位置,允许为空.") + private Double startY; + @FieldDescribe("背景是否透明,默认为false.") + private Boolean omitBackground; @FieldDescribe("工作标识,把图片保存到工单的附件中,非必填") private String workId; @FieldDescribe("位置,工作标识不为空的时候必填") @@ -212,12 +221,44 @@ class ActionHtmlToImage extends BaseAction { this.site = site; } - public Integer getQuality() { - return quality; + public Double getStartX() { + return startX == null ? 0D : startX; + } + + public void setStartX(Double startX) { + this.startX = startX; + } + + public Double getStartY() { + return startY == null ? 0D : startX; + } + + public void setStartY(Double startY) { + this.startY = startY; + } + + public Double getHtmlWidth() { + return htmlWidth; + } + + public void setHtmlWidth(Double htmlWidth) { + this.htmlWidth = htmlWidth; + } + + public Double getHtmlHeight() { + return htmlHeight; + } + + public void setHtmlHeight(Double htmlHeight) { + this.htmlHeight = htmlHeight; + } + + public Boolean getOmitBackground() { + return omitBackground; } - public void setQuality(Integer quality) { - this.quality = quality; + public void setOmitBackground(Boolean omitBackground) { + this.omitBackground = omitBackground; } } @@ -240,10 +281,11 @@ class ActionHtmlToImage extends BaseAction { try (Browser browser = browserType.launch(options)) { BrowserContext context = browser.newContext(); Page page = context.newPage(); - page.navigate("file:///Users/chengjian/dev/temp/test.html"); + page.navigate("file:///Users/chengjian/Downloads/test11.html"); Page.ScreenshotOptions screenshotOptions = new Page.ScreenshotOptions(); screenshotOptions.setFullPage(true); screenshotOptions.setClip(0,0, 800, 2310); + screenshotOptions.setOmitBackground(true); //screenshotOptions.setQuality(2); screenshotOptions.setPath(Paths.get("/Users/chengjian/dev/temp/screenshot-" + browserType.name() + ".png")); page.screenshot(screenshotOptions); 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 de65af1d40a41855b2d484038ae1c8e355c80568..a2b225a0928c761611e94546400d276ee11a56b2 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 @@ -1348,7 +1348,8 @@ public class AttachmentAction extends StandardJaxrsAction { asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); } - @JaxrsMethodDescribe(value = "html转图片工具类,转换后如果工作不为空通过downloadWithWork接口下载,为空downloadTransfer接口下载.", action = ActionHtmlToImage.class) + @JaxrsMethodDescribe(value = "html转图片工具类,通过微软playwright工具以截图方式生成图片," + + "转换后如果工作不为空通过downloadWithWork接口下载,为空downloadTransfer接口下载.", action = ActionHtmlToImage.class) @POST @Path("html/to/image") @Produces(HttpMediaType.APPLICATION_JSON_UTF_8)