zoukankan      html  css  js  c++  java
  • 在使用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

    我是个双鱼座的小王子,沉浸在自己的代码世界里,去探索这未知的世界,希望遇到更多的小伙伴一起前行!
  • 相关阅读:
    d2admin框架学习
    手机访问本地配置域名下的项目
    laydate 限制结束日期不能大于起始日期
    学习MVC之租房网站(十一)-定时任务和云存储
    学习MVC之租房网站(十)-预约和跟单
    学习MVC之租房网站(九)-房源显示和搜索
    学习MVC之租房网站(八)- 前台注册和登录
    学习MVC之租房网站(七)-房源管理和配图上传
    《程序员修炼之道》笔记(九)
    《程序员修炼之道》笔记(八)
  • 原文地址:https://www.cnblogs.com/zxy-come-on/p/15448781.html
Copyright © 2011-2022 走看看