【发布时间】:2018-07-04 21:22:03
【问题描述】:
将 HTML 转换为 PDF 时出错
错误:'元素类型“链接”必须由匹配的结束标记“”终止。' org.xhtmlrenderer.util.XRRuntimeException:无法加载 XML 资源(使用 TrAX 转换器)。 org.xml.sax.SAXParseException;行号:9;列号:3;元素类型“link”必须以匹配的结束标签“”结束。
该项目基于 Springboot 和 Thymeleaf。
Springboot 通过一系列复选框为用户提供一个表单。表单如下所示:
当表单数据被提交到后端时,它会被放回 HTML 表单的副本中,以便呈现为 PDF。我正在使用here 概述的技术。
POST 控制器:
@PostMapping("/assessment")
public String greetingSubmit(@ModelAttribute("assessemnt") Assessment assessment, BindingResult result, Model model,
HttpServletRequest request, SessionStatus status) throws IOException {
// GET A LIST OF ATTRIBUTES IN CLASS AND PUT IN MAP
Map<String, Object> map = oMapper.convertValue(assessment, Map.class);
Iterator it = map.entrySet().iterator();
Map<String,String> data = new HashMap<String,String>();
it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if (pair.getValue() instanceof Boolean) {
if (pair.getValue().equals(true)) {
data.put(pair.getKey().toString(), "checked");
}
}
}
try {
pdfGenaratorUtil.createPdf("assessmenttemplate.html",data);
} catch (Exception e) {
e.printStackTrace();
}
还有评估模板.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Assessment</title>
<link rel="stylesheet" type="text/css" href="/css/assessment.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/flatly/bootstrap.min.css">
</head>
<body>
<form action="#" th:action="@{/greeting}" method="post">
<div class="container">
<div class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="#">Bootstrap 24445 + Flatly theme</a>
</div>
</div>
<div class="panel-group" id="accordion">
<div class="panel panel-default" id="panel1">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-target="#section1" href="#collapseOne">
Collapsible Group Item #1
</a>
</h4>
</div>
<div id="section1" class="panel-collapse collapse">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,
raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
<div class="form-check form-check-inline">
<input class="form-check-input checkbox1" type="checkbox" id="section1Checkbox1" value="option1" th:field="*{needsAssessment.section1Checkbox1}">
<label class="form-check-label" for="section1Checkbox1">Checkbox 1</label>
</div>
当行
pdfGenaratorUtil.createPdf("assessmenttemplate.html",data);
正在运行堆栈跟踪
ERROR: '元素类型“link”必须由匹配终止 结束标签“”。 org.xhtmlrenderer.util.XRRuntimeException:不能 加载 XML 资源(使用 TrAX 转换器)。 org.xml.sax.SAXParseException;行号:9;列号:3;这 元素类型“链接”必须由匹配的结束标签终止 “”。
错误很明显 - 但如果我将assessmenttemple.html 上传到 w3 验证器,它会正确地抱怨 th 标签,但没有关闭链接标签的问题。
【问题讨论】:
-
那么,如果 w3 验证器说不同的话会怎样。 PDF 渲染器需要 XML 格式的 HTML,而 w3 验证器不需要。忽略 w3 验证器所说的内容,并修复 PDF 渲染器识别的问题,因为 it 是需要处理 HTML 的那个。 --- 正如你所说的“错误很明显”,那你为什么不把它修好呢?
标签: java html spring itext thymeleaf