Question

I am trying to use flyingsaucer to serve pdf generated from xhtml but I am having trouble getting the servlet example to run.

All the other flyingsaucer examples work fine for me but I need this to work as a servlet to incorporate into a webapp.

The full code for the servlet is as follows:

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";
    }
}

I get this exception:

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)
    ...

It doesn't run beyond this line (in the try-catch block):

renderer.setDocument(doc, null);

I tried a few things like changing the input stream type and verifying that the xhtml ran OK but that did not resolve anything.

I am not all that familiar with Java servlets so I am not sure if I am addressing the right problem, it seems that I need to work out some way to keep the input stream from closing before I run this:

renderer.setDocument(doc, null);

Is this possible or should I be addressing something else?

I am using Tomcat 7 and Java 6. I noticed other people had a similar problem but I only get it when running the servlet example - all other examples run fine.

Was it helpful?

Solution

String css = getServletContext().getRealPath("/PDFservlet.css");

This is not right. It has to be an URL, not a local disk file system path. IText is attempting to download it by an URL "the usual way", like as a webbrowser would do.

One of the ways to construct the proper URL would be this:

StringBuffer url = req.getRequestURL();
String base = url.substring(0, url.length() - req.getRequestURI().length() + req.getContextPath().length());
String css = base + "/PDFservlet.css";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top