我正在尝试使用flyingsaucer来提供从xhtml生成的pdf,但是我无法运行servlet示例。
所有其他flyingsaucer
示例对我来说都很好,但是我需要将它作为servlet来合并到webapp中。
servlet的完整代码如下:
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class PDFServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/pdf");
StringBuffer buf = new StringBuffer();
buf.append("<html>");
String css = getServletContext().getRealPath("/PDFservlet.css");
System.out.println("css url 2= " + css);
// put in some style
buf.append("<head><link rel='stylesheet' type='text/css' "+
"href='"+css+"' media='print'/></head>");
buf.append("<body>");
buf.append("<h1>Quarterly Reports for " +
request.getParameter("username")+"</h1>");
buf.append("<table cellspacing='0'>");
buf.append("<tr><th>Sales</th><th>Profit</th><th>Bonus</th></tr>");
// generate sales data
int totalSales = 0;
int totalProfit = 0;
int totalBonus = 0;
for(int i=0; i<10; i++) {
int currentSales = (int)(Math.random()*10000);
int currentProfit = (int)(currentSales*0.2);
int currentBonus = (int)(currentProfit*0.33);
buf.append("<tr><td>"+currentSales+"$</td><td>"+
currentProfit+"$</td><td>"+currentBonus+"$</td></tr>");
totalSales += currentSales;
totalProfit += currentProfit;
totalBonus += currentBonus;
}
buf.append("<tr class='total-header'><td colspan='3'>totals</td></tr>");
buf.append("<tr class='total'><td>"+totalSales+"$</td><td>"+
totalProfit+"$</td><td>"+totalBonus+"$</td></tr>");
buf.append("</table>");
buf.append("</body>");
buf.append("</html>");
byte[] byteArray = buf.toString().getBytes("ISO-8859-1");
// parse our markup into an xml Document
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
Document doc = builder.parse(baos);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
OutputStream os = response.getOutputStream();
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
我得到了这个例外:
Jan 17, 2013 7:55:23 PM org.xhtmlrenderer.util.XRLog log
WARNING: Unhandled exception. IOException on parsing style seet from a Reader; don't know the URI.
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at org.xhtmlrenderer.css.parser.Lexer.zzRefill(Lexer.java:1527)
...
at org.xhtmlrenderer.context.StyleReference.readAndParseAll(StyleReference.java:122)
at org.xhtmlrenderer.context.StyleReference.setDocumentContext(StyleReference.java:106)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:130)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:106)
at PDFServlet.processRequest(PDFServlet.java:73)
at PDFServlet.doGet(PDFServlet.java:75)
...
它不会超出此行(在try-catch块中):
renderer.setDocument(doc, null);
我尝试了一些方法,例如更改输入流类型并验证xhtml运行正常,但是没有解决任何问题。
我并不熟悉Java servlet所以我不确定我是否正在解决正确的问题,似乎我需要找到一些方法来保持输入流不会在运行之前关闭:
renderer.setDocument(doc, null);
这是可能的,还是应该解决其他问题?
我正在使用Tomcat 7和Java 6.我注意到其他人有一个similar problem但我只在运行servlet示例时才得到它 - 所有其他示例运行正常。
答案 0 :(得分:4)
String css = getServletContext().getRealPath("/PDFservlet.css");
这是不对的。它必须是URL,而不是本地磁盘文件系统路径。 IText正试图通过URL“通常的方式”下载它,就像webbrowser那样。
构建正确URL的方法之一是:
StringBuffer url = req.getRequestURL();
String base = url.substring(0, url.length() - req.getRequestURI().length() + req.getContextPath().length());
String css = base + "/PDFservlet.css";