java-xhtml到带有flyingsaucer的pdf servlet
作者:互联网
我正在尝试使用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和Java6.我注意到其他人使用的是similar problem,但是我只有在运行servlet示例时才知道它-其他所有示例都运行良好.
解决方法:
String css = getServletContext().getRealPath("/PDFservlet.css");
这是不对的.它必须是URL,而不是本地磁盘文件系统路径. IText尝试通过URL“通常的方式”下载它,就像Web浏览器一样.
构造适当的URL的方法之一是:
StringBuffer url = req.getRequestURL();
String base = url.substring(0, url.length() - req.getRequestURI().length() + req.getContextPath().length());
String css = base + "/PDFservlet.css";
标签:servlets,pdf-generation,flying-saucer,java 来源: https://codeday.me/bug/20191031/1975290.html