这是我的 JSP 页面。单击按钮时,我将调用此 JSP 页面,它将 HTML 文档转换为 PDF 文档。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.io.*,com.itextpdf.text.*,com.itextpdf.tool.xml.*,com.itextpdf.text.pdf.PdfWriter"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:/Documents and Settings/preetish/Desktop/practice/Buttons.html"));
//step 5
document.close();
%>
</body>
</html>
但是当我在 Apache Tomcat 服务器上运行 index.jsp 时,它给了我以下错误日志:
SEVERE: Servlet.service() for servlet [jsp] in context with path [/paginanation] threw exception [An exception occurred processing JSP page /pfd.jsp at line 19
16: // step 3
17: document.open();
18: // step 4
19: XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:/Documents and Settings/preetish/Desktop/practice/Buttons.html"));
20: //step 5
21: document.close();
22:
Stacktrace:] with root cause
java.lang.ClassCastException: Insertion of illegal Element: 30
at com.itextpdf.text.Phrase.add(Phrase.java:367)
at com.itextpdf.text.Paragraph.add(Paragraph.java:345)
at com.itextpdf.tool.xml.html.Div.end(Div.java:117)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:174)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:223)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:185)
at org.apache.jsp.pfd_jsp._jspService(pfd_jsp.java:76)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)