问题描述
- com.itextpdf.tool.xml.exceptions.
-
如题,在做html转换成pdf时出现这个异常,求大神帮助。我没有金币了,如果有就悬赏了!
Exception in thread "main" com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag meta.
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:135)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:397)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:71)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:237)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:215)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:204)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:237)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:211)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:251)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:199)
at com.hejinonline.pdf.HtmlToPdfTest.createPdf(HtmlToPdfTest.java:71)
at com.hejinonline.pdf.HtmlToPdfTest.main(HtmlToPdfTest.java:83)
解决方案
这是代码: public static final String HTML = "E:/1.html";
public static final String DEST = "E:/1.pdf";
/**
* Creates a PDF with the words "Hello World"
* @param file
* @throws IOException
* @throws DocumentException
*/
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML), Charset.forName("UTF-8"));
// step 5
document.close();
}
/**
* Main method
*/
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new HtmlToPdfTest().createPdf(DEST);
}
HTML
<html>
.b1{white-space-collapsing:preserve;}<br>
.b2{margin: 0.9847222in 1.0236111in 0.9847222in 1.0236111in;}<br>
.s1{font-weight:bold;}<br>
.p1{text-align:center;hyphenate:auto;font-family:Times New Roman;font-size:16pt;}<br>
.p2{text-align:center;hyphenate:auto;font-family:仿宋_GB2312;font-size:15pt;}<br>
.p3{text-align:center;hyphenate:auto;font-family:楷体_GB2312;font-size:12pt;}<br>
.p4{text-align:justify;hyphenate:auto;font-family:楷体_GB2312;font-size:10pt;}<br>
.p5{text-align:start;hyphenate:auto;font-family:Times New Roman;font-size:10pt;}<br>
.p6{text-indent:0.29166666in;text-align:justify;hyphenate:auto;font-family:Times New Roman;font-size:10pt;}<br>
.td1{width:1.1583333in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td2{width:0.8055556in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td3{width:1.1631944in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td4{width:2.7909722in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td5{width:1.96875in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td6{width:0.8263889in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td7{width:1.9645833in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td8{width:4.759722in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.r1{height:0.39375in;keep-together:always;}<br>
.r2{height:1.9708333in;keep-together:always;}<br>
.r3{height:0.67777777in;keep-together:always;}<br>
.r4{height:1.2145833in;keep-together:always;}<br>
.r5{height:1.2361112in;keep-together:always;}<br>
.r6{height:1.2743056in;keep-together:always;}<br>
.t1{table-layout:fixed;border-collapse:collapse;border-spacing:0;}<br>
计算机科学系毕业生论文(设计)管理规定
计算机科学与技术学院
实习答辩请假条
学生姓名 | 专业班级 | |||
学号 | 联系电话 | |||
请假原因 | ||||
请假时间段
(不超过7天) |
年 月 日至 年 月 日 | |||
指导教
师意见 |
签名:
年 月 日 |
|||
辅导员
意见 |
签名:
年 月 日 |
|||
系主任意见 | 签名:
年 月 日 |
注:1、请假时需持研究生复试通知单或就聘单位面试通知。
2、该假条一式两份,一份交至专业负责人处,一份交至辅导员处。
解决方案二:
这是代码: public static final String HTML = "E:/1.html";
public static final String DEST = "E:/1.pdf";
/**
* Creates a PDF with the words "Hello World"
* @param file
* @throws IOException
* @throws DocumentException
*/
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML), Charset.forName("UTF-8"));
// step 5
document.close();
}
/**
* Main method
*/
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new HtmlToPdfTest().createPdf(DEST);
}
HTML
<html>
.b1{white-space-collapsing:preserve;}<br>
.b2{margin: 0.9847222in 1.0236111in 0.9847222in 1.0236111in;}<br>
.s1{font-weight:bold;}<br>
.p1{text-align:center;hyphenate:auto;font-family:Times New Roman;font-size:16pt;}<br>
.p2{text-align:center;hyphenate:auto;font-family:仿宋_GB2312;font-size:15pt;}<br>
.p3{text-align:center;hyphenate:auto;font-family:楷体_GB2312;font-size:12pt;}<br>
.p4{text-align:justify;hyphenate:auto;font-family:楷体_GB2312;font-size:10pt;}<br>
.p5{text-align:start;hyphenate:auto;font-family:Times New Roman;font-size:10pt;}<br>
.p6{text-indent:0.29166666in;text-align:justify;hyphenate:auto;font-family:Times New Roman;font-size:10pt;}<br>
.td1{width:1.1583333in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td2{width:0.8055556in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td3{width:1.1631944in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td4{width:2.7909722in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td5{width:1.96875in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td6{width:0.8263889in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td7{width:1.9645833in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.td8{width:4.759722in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}<br>
.r1{height:0.39375in;keep-together:always;}<br>
.r2{height:1.9708333in;keep-together:always;}<br>
.r3{height:0.67777777in;keep-together:always;}<br>
.r4{height:1.2145833in;keep-together:always;}<br>
.r5{height:1.2361112in;keep-together:always;}<br>
.r6{height:1.2743056in;keep-together:always;}<br>
.t1{table-layout:fixed;border-collapse:collapse;border-spacing:0;}<br>
计算机科学系毕业生论文(设计)管理规定
计算机科学与技术学院
实习答辩请假条
学生姓名 | 专业班级 | |||
学号 | 联系电话 | |||
请假原因 | ||||
请假时间段
(不超过7天) |
年 月 日至 年 月 日 | |||
指导教
师意见 |
签名:
年 月 日 |
|||
辅导员
意见 |
签名:
年 月 日 |
|||
系主任意见 | 签名:
年 月 日 |
注:1、请假时需持研究生复试通知单或就聘单位面试通知。
2、该假条一式两份,一份交至专业负责人处,一份交至辅导员处。
解决方案三:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">.b1{white-space-collapsing:preserve;}
.b2{margin: 0.9847222in 1.0236111in 0.9847222in 1.0236111in;}
.s1{font-weight:bold;}
.p1{text-align:center;hyphenate:auto;font-family:Times New Roman;font-size:16pt;}
.p2{text-align:center;hyphenate:auto;font-family:仿宋_GB2312;font-size:15pt;}
.p3{text-align:center;hyphenate:auto;font-family:楷体_GB2312;font-size:12pt;}
.p4{text-align:justify;hyphenate:auto;font-family:楷体_GB2312;font-size:10pt;}
.p5{text-align:start;hyphenate:auto;font-family:Times New Roman;font-size:10pt;}
.p6{text-indent:0.29166666in;text-align:justify;hyphenate:auto;font-family:Times New Roman;font-size:10pt;}
.td1{width:1.1583333in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td2{width:0.8055556in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td3{width:1.1631944in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td4{width:2.7909722in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td5{width:1.96875in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td6{width:0.8263889in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td7{width:1.9645833in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.td8{width:4.759722in;padding-start:0.075in;padding-end:0.075in;border-bottom:thin solid black;border-left:thin solid black;border-right:thin solid black;border-top:thin solid black;}
.r1{height:0.39375in;keep-together:always;}
.r2{height:1.9708333in;keep-together:always;}
.r3{height:0.67777777in;keep-together:always;}
.r4{height:1.2145833in;keep-together:always;}
.r5{height:1.2361112in;keep-together:always;}
.r6{height:1.2743056in;keep-together:always;}
.t1{table-layout:fixed;border-collapse:collapse;border-spacing:0;}
</style>
<title>计算机科学系毕业生论文(设计)管理规定</title>
<meta content="xie" name="author">
</head>
<body class="b1 b2">
<p class="p1"> <span class="s1">计算机科学与技术学院</span> </p>
<p class="p1"> <span class="s1">实习答辩请假条</span> </p>
<p class="p2"></p>
<table class="t1">
<tbody>
<tr class="r1">
<td class="td1"> <p class="p3"> <span>学生姓名</span> </p> </td>
<td class="td2"> <p class="p3"></p> </td>
<td class="td3"> <p class="p3"> <span>专业班级</span> </p> </td>
<td class="td4" colspan="2"> <p class="p3"></p> </td>
</tr>
<tr class="r1">
<td class="td1"> <p class="p3"> <span>学号</span> </p> </td>
<td class="td5" colspan="2"> <p class="p3"></p> </td>
<td class="td6"> <p class="p3"> <span>联系电话</span> </p> </td>
<td class="td7"> <p class="p3"></p> </td>
</tr>
<tr class="r2">
<td class="td1"> <p class="p3"> <span>请假原因</span> </p> </td>
<td class="td8" colspan="4"> <p class="p3"></p> </td>
</tr>
<tr class="r3">
<td class="td1"> <p class="p3"> <span>请假时间段</span> </p> <p class="p4"> <span>(不超过7天)</span> </p> </td>
<td class="td8" colspan="4"> <p class="p3"> <span>年 月 日至 年 月 日</span> </p> </td>
</tr>
<tr class="r4">
<td class="td1"> <p class="p3"> <span>指导教</span> </p> <p class="p3"> <span>师意见</span> </p> </td>
<td class="td8" colspan="4"> <p class="p3"></p> <p class="p3"></p> <p class="p3"></p> <p class="p3"> <span> 签名:</span> </p> <p class="p3"> <span> 年 月 日</span> </p> </td>
</tr>
<tr class="r5">
<td class="td1"> <p class="p3"> <span>辅导员</span> </p> <p class="p3"> <span>意见</span> </p> </td>
<td class="td8" colspan="4"> <p class="p3"></p> <p class="p3"></p> <p class="p3"></p> <p class="p3"> <span> 签名:</span> </p> <p class="p3"> <span> 年 月 日</span> </p> </td>
</tr>
<tr class="r6">
<td class="td1"> <p class="p3"> <span>系主任意见</span> </p> </td>
<td class="td8" colspan="4"> <p class="p3"></p> <p class="p3"></p> <p class="p3"> <span> </span> </p> <p class="p3"> <span> 签名:</span> </p> <p class="p3"> <span> 年 月 日</span> </p> </td>
</tr>
</tbody>
</table>
<p class="p5"> <span>注:1、请假时需持研究生复试通知单或就聘单位面试通知。</span> </p>
<p class="p5"> <span>2、该假条一式两份,一份交至专业负责人处,一份交至辅导员处。</span> </p>
<p class="p6"></p>
</body>
</html>