-1

这是我的 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)
4

2 回答 2

1

您忘记导入 Document 类。

<% page import="class">

这是在jsp页面中导入的方式

于 2013-09-05T11:59:03.843 回答
0

您自己正在使用松散的 HTML4(至少在 JSP 中),并且正在尝试转换 XHTML。

  • 一般来说:制作一个独立的应用程序(库)以便于开发。
  • 将原始 HTML4 页面转换为 XHTML,并验证页面(Tiny Validator,任何浏览器插件。)

该错误可能暗示了一个非法元素,例如

<b><p>lorem</p><p>ipsum</p></b>

b-tag 中不允许使用 p-tag。

如您所知,XHTML 需要将单例编写为<img .../>. 对于 HTML,您可以在/>. 打开标签<p>需要一个关闭标签</p>

于 2013-09-05T12:16:43.153 回答