xhtmlrenderer PDF Servlet Filter

I managed to get xhtmlrenderer working as a Servlet Filter, so that any HTML page you apply the filter to will be converted to PDF. Here are a few hints:

  • If you want to keep the XHTML in memory rather than writing it out to a file, then you’ll need an XML parser like Xerces to parse the XHTML into a DOM object before you can pass it to xhtmlrenderer. (iText has got to have some kind of XML parser it uses that you could probably use too, instead of including a separate library. I just didn’t take the time to figure it out.)
  • You’ll need something like a ByteArrayServletOutputStream to capture the JSP output in-memory so you can then convert it to PDF. Catalina has one.
  • If you have a table that spans multiple pages and you want to repeat the table header and footer on each page, use the custom CSS attribute “-fs-table-paginate: paginate;” on the table.
  • Be sure to pass xhtmlrenderer the URL of the page it’s rendering, so that it can access relative URLs to pull up external resources like images and CSS files.
  • I’m using JSPs as the templating solution, and I ran into some buffering issues where no content was written into the ServletOutputStream when I was applying the filter. A manual call to out.flush() at the end of the JSP works for now, but I’m looking for a better solution.
Advertisements

One Response to xhtmlrenderer PDF Servlet Filter

  1. Gilles says:

    Hi,
    Is your code available anywhere ?
    That would be very helpful!

    Thanks
    Gilles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: