You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
8.8 KiB
202 lines
8.8 KiB
3 years ago
|
package com.fr.plugin.mail.fold;
|
||
|
|
||
|
import com.fr.base.Base64;
|
||
|
import com.fr.base.EmailAttachment;
|
||
|
import com.fr.page.web.EmailCssHolder;
|
||
|
import com.fr.stable.CommonCodeUtils;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.email.EmailAttachmentProvider;
|
||
|
import com.fr.stable.html.Tag;
|
||
|
import com.fr.web.core.ReportSessionIDInfor;
|
||
|
import com.fr.web.email.EmailPreviewContentReader;
|
||
|
|
||
|
import javax.activation.DataHandler;
|
||
|
import javax.mail.MessagingException;
|
||
|
import javax.mail.internet.MimeBodyPart;
|
||
|
import javax.mail.internet.MimeMultipart;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import java.util.*;
|
||
|
|
||
|
/**
|
||
|
* @Author: fr.open
|
||
|
* @Date: 2020/8/11 16:36
|
||
|
* 模板正文导出成html或者base64图片
|
||
|
*/
|
||
|
public class TagUtils {
|
||
|
|
||
|
public static String exportTemplateAsHtml(HttpServletRequest req, ReportSessionIDInfor sessionIDInfor) throws Exception {
|
||
|
Map<String, String> cssMap = EmailCssHolder.getInstance().getCssMap("/com/fr/web/core/css/report.core.css");
|
||
|
// Tag reportContentTag = MA.F().B(req, sessionIDInfor);
|
||
|
Tag reportContentTag = EmailPreviewContentReader.getInstance().getContentTag(req, sessionIDInfor);
|
||
|
Set<String> classGroupSet = getAllClsGroup(reportContentTag);
|
||
|
StringBuffer finalcss = new StringBuffer();
|
||
|
for (String clsGroup : classGroupSet) {
|
||
|
String[] claArr = clsGroup.split(" ");
|
||
|
String key = clsGroup.replace(" ", "");
|
||
|
StringBuffer mergecss = new StringBuffer("{");
|
||
|
for (String cls : claArr) {
|
||
|
String text = cssMap.get("." + cls);
|
||
|
if (StringUtils.isNotEmpty(text)) {
|
||
|
text = text.replace("*", ""); // outlook 网页端不兼容*号css样式,*是outlook全局样式
|
||
|
/* if (!StringUtils.contains(text,"word-break")&&!StringUtils.contains(text,"word-wrap")){
|
||
|
mergecss.append(text.substring(text.indexOf("{") + 1, text.lastIndexOf("}"))).append(";");
|
||
|
}*/
|
||
|
mergecss.append(text.substring(text.indexOf("{") + 1, text.lastIndexOf("}"))).append(";");
|
||
|
}
|
||
|
}
|
||
|
if (!"{".equals(mergecss.toString())) {
|
||
|
mergecss.append("}");
|
||
|
finalcss.append(".").append(key).append(" ").append(mergecss);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
finalcss.append("table.x-table{overflow:visible;line-height:1;}");//QQ邮箱样式影响
|
||
|
finalcss.append("td{line-height:1 !important;}");//网易邮箱样式影响
|
||
|
|
||
|
// setTdNowrap(reportContentTag);
|
||
|
|
||
|
Set<Tag> allUndisplayTag = findAllUndisplayTag(reportContentTag);
|
||
|
String contetTag = reportContentTag.toString();
|
||
|
for (Tag tag : allUndisplayTag) {
|
||
|
contetTag = contetTag.replace(tag.toString(), ""); // 把隐藏的tag不输出,outlook客户端不兼容display:none的style样式
|
||
|
}
|
||
|
String bodyContent = "<style type=\"text/css\" id=\"_fr_content_style\">" + finalcss + "</style>" + contetTag;
|
||
|
changeBase64ToPng(bodyContent);
|
||
|
return bodyContent;
|
||
|
}
|
||
|
|
||
|
|
||
|
private static Set<String> getAllClsGroup(Tag tag) {
|
||
|
Set<String> result = new HashSet<String>();
|
||
|
if (tag != null) {
|
||
|
String tagCls = tag.getClassList().toString();
|
||
|
if (StringUtils.isNotEmpty(tagCls)) {
|
||
|
result.add(tagCls);
|
||
|
}
|
||
|
List subList = tag.getSubHtmlList();
|
||
|
if (subList != null) {
|
||
|
for (int i = 0; i < subList.size(); i++) {
|
||
|
Object html = subList.get(i);
|
||
|
if (html instanceof Tag) {
|
||
|
result.addAll(getAllClsGroup((Tag) html));
|
||
|
if ("td".equals(((Tag) html).getTagName())) {
|
||
|
html = new JoinedTag((Tag) html);
|
||
|
subList.set(i, html);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
private static Set<Tag> findAllUndisplayTag(Tag tag) {
|
||
|
Set<Tag> result = new HashSet();
|
||
|
if (tag != null) {
|
||
|
String tagStyle = tag.getStyleList().toString();
|
||
|
if (StringUtils.contains(tagStyle, "display:none")) {
|
||
|
result.add(tag);
|
||
|
}
|
||
|
|
||
|
List subList = tag.getSubHtmlList();
|
||
|
if (subList != null) {
|
||
|
for (int i = 0; i < subList.size(); i++) {
|
||
|
Object html = subList.get(i);
|
||
|
if (html instanceof Tag) {
|
||
|
result.addAll(findAllUndisplayTag((Tag) html));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
//((Tag) html).setAttributes((Map<String, String>) new HashMap().put("nowrap","nowrap"));
|
||
|
private static void setTdNowrap(Tag tag) {
|
||
|
if (tag != null) {
|
||
|
if ("td".equals(tag.getTagName())) {
|
||
|
tag.setAttributes((Map<String, String>) new HashMap().put("nowrap", "nowrap"));
|
||
|
}
|
||
|
List subList = tag.getSubHtmlList();
|
||
|
if (subList != null) {
|
||
|
for (int i = 0; i < subList.size(); i++) {
|
||
|
Object html = subList.get(i);
|
||
|
if (html instanceof Tag) {
|
||
|
setTdNowrap((Tag) html);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
public static EmailAttachment[] buildBase64ToPngEmailAttachment(String bodyContent) throws MessagingException {
|
||
|
ArrayList emailAttachmentsGroup = new ArrayList<EmailAttachment>();
|
||
|
String defaultName = "templateImage";
|
||
|
String base64Tag = "data:image/png;base64,";
|
||
|
MimeMultipart multipart = new MimeMultipart();
|
||
|
if (bodyContent.contains(base64Tag)) {
|
||
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
||
|
while (bodyContent.contains(base64Tag)) {
|
||
|
int start = bodyContent.indexOf(base64Tag);
|
||
|
int end = bodyContent.indexOf("></img>", start);
|
||
|
String base64 = bodyContent.substring(start + base64Tag.length(), end);
|
||
|
String cidPrefix = defaultName+ UUID.randomUUID();
|
||
|
bodyContent = bodyContent.substring(0, start) + "cid:" + cidPrefix + "\"" + bodyContent.substring(end);
|
||
|
// 跟代码看到com.fr.base.EmailManager.sendWithRecord里面,setFormat直接给的null,这里暂时先用null
|
||
|
String setFormat = null;
|
||
|
htmlPart.setContent(bodyContent, setFormat);
|
||
|
EmailAttachment emailAttachment = new EmailAttachment(Base64.decode(CommonCodeUtils.attributeHtmlDecode(base64)), cidPrefix);
|
||
|
emailAttachmentsGroup.add(emailAttachment);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int size = emailAttachmentsGroup.size();
|
||
|
EmailAttachment[] emailAttachments = new EmailAttachment[size];
|
||
|
if (!emailAttachmentsGroup.isEmpty()){
|
||
|
for (int i=0;i<size;i++){
|
||
|
EmailAttachment attachment = (EmailAttachment) emailAttachmentsGroup.get(i);
|
||
|
emailAttachments[i] = attachment;
|
||
|
}
|
||
|
}
|
||
|
return emailAttachments;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static MimeMultipart changeBase64ToPng(String bodyContent) throws MessagingException {
|
||
|
String defaultName = "templateImage";
|
||
|
String base64Tag = "data:image/png;base64,";
|
||
|
MimeMultipart multipart = new MimeMultipart();
|
||
|
if (bodyContent.contains(base64Tag)) {
|
||
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
||
|
while (bodyContent.contains(base64Tag)) {
|
||
|
int start = bodyContent.indexOf(base64Tag);
|
||
|
int end = bodyContent.indexOf("></img>", start);
|
||
|
String base64 = bodyContent.substring(start + base64Tag.length(), end);
|
||
|
String cidPrefix = defaultName+ UUID.randomUUID();
|
||
|
bodyContent = bodyContent.substring(0, start) + "cid:" + cidPrefix + "\"" + bodyContent.substring(end);
|
||
|
// 跟代码看到com.fr.base.EmailManager.sendWithRecord里面,setFormat直接给的null,这里暂时先用null
|
||
|
String setFormat = null;
|
||
|
htmlPart.setContent(bodyContent, setFormat);
|
||
|
|
||
|
addImagePart(multipart, new EmailAttachment(Base64.decode(CommonCodeUtils.attributeHtmlDecode(base64)), cidPrefix), cidPrefix);
|
||
|
}
|
||
|
multipart.addBodyPart(htmlPart);
|
||
|
|
||
|
}
|
||
|
return multipart;
|
||
|
}
|
||
|
|
||
|
private static void addImagePart(MimeMultipart mimeMultipart, EmailAttachmentProvider provider, String filename) throws MessagingException {
|
||
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
||
|
mimeBodyPart.setDataHandler(new DataHandler(provider.getByteArrayDataSource()));
|
||
|
mimeBodyPart.setHeader("Content-ID", "<" + filename + ">");
|
||
|
mimeBodyPart.setFileName(filename + ".png");
|
||
|
mimeBodyPart.setDisposition("inline");
|
||
|
mimeMultipart.addBodyPart(mimeBodyPart);
|
||
|
}
|
||
|
|
||
|
}
|