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

    我是个双鱼座的小王子,沉浸在自己的代码世界里,去探索这未知的世界,希望遇到更多的小伙伴一起前行!
  • 相关阅读:
    [python]python学习笔记(七)——加密
    Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音 .
    Android 关于汉字转拼音的工具类Pinyin4jUtil 的使用说明
    20条技巧,让Chrome超越Firefox
    Android 内存优化
    WebView详解
    Android 获取手机通讯录信息 — 姓名和号码
    Android 获取手机通讯录信息 — 头像、姓名和A-Z的快速查询
    Android 快速开发框架AFinal
    Android 滑动改变视频音量和视频缩略图
  • 原文地址:https://www.cnblogs.com/zxy-come-on/p/15448781.html
Copyright © 2011-2022 走看看