当前位置:网站首页>下载(导出)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();
}
}
边栏推荐
- 线程的并行、并发、生命周期
- Lake Shore 连续流动低温恒温器传输线
- Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
- Lumiprobe free radical analysis h2dcfda instructions
- 助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
- M91快速霍尔测量仪—在更短的时间内进行更好的测量
- 【森城市】GIS数据漫谈(一)
- 案例分享:QinQ基本组网配置
- Example explanation: move graph explorer to jupyterlab
- Improve yolov5 with gsconv+slim neck to maximize performance!
猜你喜欢
网易游戏,激进出海
Lumiprobe free radical analysis h2dcfda instructions
Lake Shore—OptiMag 超导磁体系统 — OM 系列
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
Getting started with kubernetes command (namespaces, pods)
ECS summer money saving secret, this time @ old users come and take it away
案例分享:QinQ基本组网配置
宝,运维100+服务器很头疼怎么办?用行云管家!
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
【直播预约】数据库OBCP认证全面升级公开课
随机推荐
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
ubuntu14安装MySQL并配置root账户本地与远程访问
精益思想:来源,支柱,落地。看了这篇文章就懂了
Openai video pre training (VPT): action learning based on watching unmarked online videos
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
Mipi interface, DVP interface and CSI interface of camera [easy to understand]
C端梦难做,科大讯飞靠什么撑起10亿用户目标?
C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
Huawei cloud experts explain the new features of gaussdb (for MySQL)
一次SQL优化,数据库查询速度提升 60 倍
Shell array
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Clean up system cache and free memory under Linux
MATLAB中subplot函数的使用
Cache problems after app release
M91 fast hall measuring instrument - better measurement in a shorter time
Docker deploy mysql8.0
Gameframework eating guide