当前位置:网站首页>下载(导出)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();
}
}
边栏推荐
- 【快应用】Win7系统使用华为IDE无法运行和调试项目
- 论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
- 一次SQL优化,数据库查询速度提升 60 倍
- Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
- SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
- Learn MySQL from scratch - database and data table operations
- Lumiprobe phosphide hexaethylene phosphide specification
- Write it down once Net travel management background CPU Explosion Analysis
- Shell array
- Contos 7 set up SFTP to create users, user groups, and delete users
猜你喜欢

生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度

nacos启动失败问题解决与总结

Example explanation: move graph explorer to jupyterlab

一次SQL优化,数据库查询速度提升 60 倍

Lumiprobe 自由基分析丨H2DCFDA说明书

中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒

Dom4J解析XML、Xpath检索XML
![[to.Net] C set class source code analysis](/img/59/4c7b910bc6505e5f81015ce80812fc.png)
[to.Net] C set class source code analysis

水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益

Getting started with kubernetes command (namespaces, pods)
随机推荐
XML语法、约束
Games202 operation 0 - environment building process & solving problems encountered
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
从零开始学 MySQL —数据库和数据表操作
Lake Shore—CRX-EM-HF 型低温探针台
[6.24-7.1] review of wonderful technical blog posts in the writing community
Learning notes - steps of JDBC connection database operation
物联网平台thingsboard搭建学习记录
M91 fast hall measuring instrument - better measurement in a shorter time
Love business in Little Red Book
前4A高管搞代运营,拿下一个IPO
Lake shore optimag superconducting magnet system om series
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
PostgreSQL varchar[] array type operation
Huawei cloud experts explain the new features of gaussdb (for MySQL)
Enabling "new Chinese enterprises", SAP process automation landing in China
PostgreSQL varchar[] 数组类型操作
Graduation summary