让Flying saucer支持font定义

在wangEditor里给某段字体加上颜色后,在生成的PDF里无法体现出来,只好打开flying saucer的源码debug,发现XhtmlNamespaceHandler类中没有对font元素的处理,于是依样画瓢:


public String getNonCssStyling(Element e) {

    if (e.getNodeName().equals("table")) {

        return applyTableStyles(e);            

    } else if (e.getNodeName().equals("td") || e.getNodeName().equals("th")) {

        return applyTableCellStyles(e);

    } else if (e.getNodeName().equals("tr")) {

        return applyTableRowStyles(e);

    } else if (e.getNodeName().equals("img")) {

        return applyImgStyles(e);

    } else if (e.getNodeName().equals("p") || e.getNodeName().equals("div")) {

        return applyBlockAlign(e);

    }else if(e.getNodeName().equalsIgnoreCase("font")){

        return applyFontStyle(e);

    }

    return "";

}


然后加上下面的方法:


private String applyFontStyle(Element e) {

        StringBuffer style = new StringBuffer();

        String s;

        s = getAttribute(e, "color");

        if (s != null) {

            s = s.toLowerCase().trim();

            style.append("color: ");

            style.append(s);

            style.append(";");

        }

        s = getAttribute(e, "size");

        if (s != null) {

            s = s.toLowerCase().trim();

            if(!s.endsWith("px")){

                s = s + "px";

            }

            style.append("font-size: ");

            style.append(s);

            style.append(";");

        }

        return style.toString();

    }


posted on 2016-11-25 22:23  秀泉  阅读(621)  评论(0编辑  收藏  举报

导航