这是我的JSP代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.lowagie.text.DocumentException" %>
<%@ page import="org.xhtmlrenderer.pdf.ITextRenderer" %>
<%@ page import="java.io.FileNotFoundException" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.File" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
final ITextRenderer iTextRenderer = new ITextRenderer();
iTextRenderer.setDocument("http://localhost:8080/MyApp/test.jsp");
iTextRenderer.layout();
final FileOutputStream fileOutputStream =new FileOutputStream(new File("invoice.pdf"));
iTextRenderer.createPDF(fileOutputStream);
fileOutputStream.close();
%>
</body>
</html>
当我运行时,我得到错误.. 。
这是完整的堆栈跟踪......
org.xhtmlrenderer.util.XRRuntimeException: Could not instantiate any SAX 2 parser, including JDK default. The name of the class to use should have been read from the org.xml.sax.driver System property, which is set to:
org.xhtmlrenderer.resource.XMLResource.newXMLReader(XMLResource.java:153)
org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:170)
org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:211)
org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134)
org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138)
org.apache.jsp.pdftest_jsp._jspService(pdftest_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
如何解决错误?
答案 0 :(得分:0)
这是错误:
XRRuntimeException:无法实例化任何SAX 2解析器,包括 JDK默认。应该读取要使用的类的名称 org.xml.sax.driver系统属性,设置为:
解决方案是定义org.xml.sax.driver。例如,下载Xerces并在Tomcat中设置此JVM选项(或者您正在使用的任何内容):
-Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser
此处有更多详情:http://www.ibm.com/developerworks/xml/library/x-tipsaxp/