当前位置:网站首页>下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
2022-07-01 18:44:00 【云南吴小黑】
- 前言,接着上一篇(下载(导出)pdf模板文件(比如:审批单));
- 报错原因:html格式有误,结束标签丢失(可能根据实际情况,确认html也是正确的,但是还是会报这个错);
- 解决思路:在拿到html内容时,通过jsoup格式化html内容;
- pom引入jsoup包
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
- 在java代码中实现html格式化
/** * 将html 转换成为严格的XHTML */
public class Html2Xhtml {
/** * 转化类 * * @param htmlPath html文件输入路径(带文件名称) * @param xhtmlPath xhtml文件输入路径(带文件名称) * @return */
public static String html2Xhtml(String htmlPath, String xhtmlPath) {
if (StringUtils.isEmpty(htmlPath)) {
return null;
}
String path = null;
try (FileInputStream fin = new FileInputStream(htmlPath)) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
int data = -1;
while ((data = fin.read()) != -1) {
byteArrayOut.write(data);
}
fin.close();
String html = byteArrayOut.toString("UTF-8");
//System.out.println("原始HTML:" + html);
String newHtml = formatHtml(html);
//System.out.println("格式化后HTML:" + newHtml);
byte[] htmlFileData = newHtml.getBytes(StandardCharsets.UTF_8);
byteArrayOut.close();
ByteArrayInputStream tidyInput = new ByteArrayInputStream(htmlFileData);
ByteArrayOutputStream tidyOut = new ByteArrayOutputStream();
Tidy tidy = new Tidy();
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setShowWarnings(false);
tidy.setIndentContent(true);
tidy.setSmartIndent(true);
tidy.setIndentAttributes(false);
tidy.setMakeClean(true);
tidy.setQuiet(true);
tidy.setWord2000(true);
tidy.setXHTML(true);
tidy.setErrout(new PrintWriter(System.out));
tidy.parse(tidyInput, tidyOut);
tidyInput.close();
tidyOut.writeTo(new FileOutputStream(xhtmlPath));
tidyOut.flush();
tidyOut.close();
path = xhtmlPath;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
path = null;
}
return path;
}
/** * 格式化html * * 报错代码:Invalid nested tag *** found, expected closing tag *** * 报错原因:html转xhtml过程中,标签没有闭合 * 解决思路:通过jsoup格式化html * @param html * @return */
private static String formatHtml(String html) {
Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
document.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
return document.html();
}
}
边栏推荐
- C端梦难做,科大讯飞靠什么撑起10亿用户目标?
- 云服务器ECS夏日省钱秘籍,这次@老用户快来领走
- Dlib+Opencv库实现疲劳检测
- Yyds dry inventory ravendb start client API (III)
- Write it down once Net travel management background CPU Explosion Analysis
- [to.Net] C set class source code analysis
- M91 fast hall measuring instrument - better measurement in a shorter time
- Three ways for redis to realize current limiting
- 精益思想:来源,支柱,落地。看了这篇文章就懂了
- 【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
猜你喜欢

Openai video pre training (VPT): action learning based on watching unmarked online videos

寶,運維100+服務器很頭疼怎麼辦?用行雲管家!

案例分享:QinQ基本组网配置

从零开始学 MySQL —数据库和数据表操作
![[to.Net] C set class source code analysis](/img/59/4c7b910bc6505e5f81015ce80812fc.png)
[to.Net] C set class source code analysis

Lumiprobe 自由基分析丨H2DCFDA说明书

M91快速霍尔测量仪—在更短的时间内进行更好的测量

智慧防疫系统为建筑工地复工复产提供安全保障

Parallelism, concurrency and life cycle of threads

Helium transmission line of lake shore cryostat
随机推荐
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
从零开始学 MySQL —数据库和数据表操作
MySQL常用图形管理工具 | 黑马程序员
nacos启动失败问题解决与总结
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
Chaos engineering platform chaosblade box new heavy release
Junit单元测试框架详解
华为游戏初始化init失败,返回错误码907135000
Shell array
Contos 7 搭建sftp之创建用户、用户组以及删除用户
Lake Shore低温恒温器的氦气传输线
【森城市】GIS数据漫谈(一)
SuperVariMag 超导磁体系统 — SVM 系列
MySQL common graphics management tools | dark horse programmers
记一次 .NET 差旅管理后台 CPU 爆高分析
Lumiprobe 活性染料丨吲哚菁绿说明书
有关 M91 快速霍尔测量仪的更多信息
水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?