Browse Source

REPORT-33569 无插件PDF导出-HTML解析:块元素(如:div、p)支持background-image、background-size 属性

research/11.0
Hugh.C 5 years ago
parent
commit
7c0d5b8553
  1. 11
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  2. 40
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/utils/BackgroundUtil.java

11
fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java

@ -50,9 +50,11 @@
package com.fr.third.v2.lowagie.text.html.simpleparser;
import com.fr.third.v2.lowagie.text.html.utils.BackgroundUtil;
import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.Chunk;
@ -134,9 +136,12 @@ public class FactoryProperties {
else if (value.equalsIgnoreCase("justify"))
p.setAlignment(Element.ALIGN_JUSTIFIED);
}
if(props.hasPropertyInChain("div", "background")){
p.setBackground(props.getPropertyFromChain("div", "background"));
}
Map<String, String> bgAttrMap = BackgroundUtil.parseBgAttr4BlockTag(props);
if (null != bgAttrMap) {
for (Map.Entry<String, String> entry : bgAttrMap.entrySet()) {
p.setAttribute(entry.getKey(), entry.getValue());
}
}
if(props.hasProperty("line-height")){
p.setAttribute("line-height", props.getProperty("line-height"));
}

40
fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/utils/BackgroundUtil.java

@ -45,4 +45,44 @@ public class BackgroundUtil {
}
return backgroundRules;
}
/**
* 将块元素的背景属性转换成mp
*
* @param props
* @return
*/
public static Map<String, String> parseBgAttr4BlockTag(ChainedProperties props) {
Map<String, String> backgroundRules = new HashMap<String, String>();
String value = props.getPropertyFromChain("div", "bgcolor");
if (value != null) {
backgroundRules.put("background-color", value);
}
value = props.getPropertyFromChain("div", "background-size");
if (value != null) {
backgroundRules.put("background-size", value);
}
value = props.getPropertyFromChain("div", "background");
if (value != null) {
Map<String, String> backgroundStyles = CSSUtils.processBackground(value);
backgroundRules.putAll(backgroundStyles);
}
value = props.getPropertyFromChain("div", "background-color");
if (value != null) {
backgroundRules.put("background-color", value);
}
value = props.getPropertyFromChain("div", "background-position");
if (value != null) {
backgroundRules.put("background-position", value);
}
value = props.getPropertyFromChain("div", "background-repeat");
if (value != null) {
backgroundRules.put("background-repeat", value);
}
value = props.getPropertyFromChain("div", "background-image");
if (value != null) {
backgroundRules.put("background-image", value);
}
return backgroundRules;
}
}

Loading…
Cancel
Save