• 在使用itextpdf对富文本转pdf时遇到Invalid nested tag XX found, expected closing tag XX的错误


    发生错误的原因是手动生成的html的标签没有闭合或者语法不规范导致的,可以使用jsoup工具对html文件进行标准化处理,实现如下:

    html 可以是富文本 或者是 html 文件

    private static String formatHtml(String html) {

    String contents = html.replaceAll("src="/cds_filestorage/download-s", "src="https://orangecds.com/cds_filestorage/download-s");
    String contentss = contents.replaceAll("data-mce-src="/cds_filestorage/download-s", "data-mce-src="https://orangecds.com/cds_filestorage/download-s");
    String contentRe = contentss.replaceAll("<video.*?>.+?</video>", "");
    log.info("content2Html-转换后的html:" + contentss);
    org.jsoup.nodes.Document doc = Jsoup.parse(contentRe);
    // jsoup生成闭合标签
    doc.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
    doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
    System.out.println("----"+doc.html());
    org.jsoup.nodes.Document doc = Jsoup.parse(contentRe);

    // 去除过大的宽度
    String style = doc.attr("style");
    if ((!style.isEmpty()) && style.contains("width")) {
    doc.attr("style", "");
    }
    Elements divs = doc.select("div");
    for (Element div : divs) {
    String divStyle = div.attr("style");
    if ((!divStyle.isEmpty()) && divStyle.contains("width")) {
    div.attr("style", "");
    }
    }
    // jsoup生成闭合标签
    doc.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
    doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
    return doc.html();
    }

    输入String类型的html文本对象,返回标准的html格式的String对象。
    需要用到的jsoup包见我上传的文件
    原文链接:https://blog.csdn.net/lxh1205509119/article/details/110402366

    我是个双鱼座的小王子,沉浸在自己的代码世界里,去探索这未知的世界,希望遇到更多的小伙伴一起前行!
  • 相关阅读:
    how to use epoll with python
    tornado ioloop current和instance的一些区别
    网络标准及路由器速度
    C语言接口

    Dalvik 与 ART
    Android学习笔记47-使用HttpClient接口实现网络通信
    Android学习笔记46-使用Post方式提交数据
    Android学习笔记45-JSON数据解析(GSON方式)
    Android学习笔记44-JSON数据解析
  • 原文地址:https://www.cnblogs.com/zxy-come-on/p/15448781.html
Copyright © 2020-2023  润新知