当前位置:网站首页>下载(导出)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();
}
}
边栏推荐
- [6.24-7.1] review of wonderful technical blog posts in the writing community
- Openai video pre training (VPT): action learning based on watching unmarked online videos
- kubernetes命令入门(namespaces,pods)
- 组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
- Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
- 华为游戏初始化init失败,返回错误码907135000
- Altair HyperWorks 2022 software installation package and installation tutorial
- Enabling "new Chinese enterprises", SAP process automation landing in China
- C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
- Dom4J解析XML、Xpath检索XML
猜你喜欢

Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase

ECS summer money saving secret, this time @ old users come and take it away

The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables

【森城市】GIS数据漫谈(一)

Prices of Apple products rose across the board in Japan, with iphone13 up 19%

Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency

SuperVariMag 超导磁体系统 — SVM 系列

线程的并行、并发、生命周期

Dom4J解析XML、Xpath检索XML

Supervarimag superconducting magnet system SVM series
随机推荐
混沌工程平台 ChaosBlade-Box 新版重磅发布
Transform + ASM data
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
宝,运维100+服务器很头疼怎么办?用行云管家!
【6.24-7.1】写作社区精彩技术博文回顾
白盒加密技术浅理解
Summary of the core steps in the life cycle of beans
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
使用环信提供的uni-app Demo,快速实现一对一单聊
Lake Shore - crx-em-hf low temperature probe station
MFC中如何重绘CListCtrl的表头
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
[live broadcast appointment] database obcp certification comprehensive upgrade open class
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
M91快速霍尔测量仪—在更短的时间内进行更好的测量
transform + asm资料
June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
Junit单元测试框架详解
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration