gpt4 book ai didi

html - 如何解决ExceptionConverter com.itextpdf.text.html.simpleparser.TableWrapper cannot be cast to com.itextpdf.text.TextElementArray

转载 作者:行者123 更新时间:2023-11-27 23:46:20 28 4
gpt4 key购买 nike

我使用以下代码的目的是将 HTML 文件转换为 PDF

 try {
com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("D:\\CreateHTMLToPDF.pdf"));
document.open();
document.addAuthor("Real Gagnon");
document.addCreator("Real's HowTo");
document.addSubject("Thanks for your support");
document.addCreationDate();
document.addTitle("Please read this");

HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "<html>"+
"<head></head>"+
"<table><body>"+
"<table border='1' style='width:100%'>"+
"<tr>"+
"<th width='100%' height='30px' style='background-color:#00FF00'><h1>Result</h1></th>"+
"</tr>"+
"</table>"+
"<table border='1' style='width:100%'>"+
"<tr>"+
"<th width='100%' height='50px'></th>"+
"</tr>"+
"</table>"+
"<table border='1' style='width:100%'>"+
"<tr>"+
"<th width='30%' height='440px'></th>"+
"<th width='30%' height='440px'></th>"+
"<th width='30%' height='220px'></th>"+
"</tr>"+
"</table>"+
"<table border='1' style='width:100%'>"+
"+<tr>"+
"<th width='100%' height='200px'></th>"+
"</tr>"+
"</table>"+
"</body></table>"+
"</html>";
htmlWorker.parse(new StringReader(str));
document.close();
System.out.println("Done");
} catch(DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

当我使用 htmlWorker 对象时,它会抛出以下异常,

java.lang.ClassCastException: com.itextpdf.text.html.simpleparser.TableWrapper cannot be cast to com.itextpdf.text.TextElementArray
at com.itextpdf.text.html.simpleparser.HTMLWorker.processTable(HTMLWorker.java:599)
at com.itextpdf.text.html.simpleparser.HTMLTagProcessors$11.endElement(HTMLTagProcessors.java:363)
at com.itextpdf.text.html.simpleparser.HTMLWorker.endElement(HTMLWorker.java:238)
at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.processTag(SimpleXMLParser.java:590)
at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.go(SimpleXMLParser.java:341)
at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.parse(SimpleXMLParser.java:608)
at com.itextpdf.text.html.simpleparser.HTMLWorker.parse(HTMLWorker.java:154)
at com.project.Html2Pdf.main(HtmlToPDF.java:71)

最佳答案

您的 html 已损坏:

<html>
<head>
</head>
<table>
<body>
...
</body>
</table>
</html>

您实际上将body 包裹在table 中,但body 必须是html

的直接子元素

关于html - 如何解决ExceptionConverter com.itextpdf.text.html.simpleparser.TableWrapper cannot be cast to com.itextpdf.text.TextElementArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765128/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com