PDF Generation using CSSToXSLFO#

Having no support for tables in PDFPlugin was a bit of a show stopper here since as well as wiki tables going missing, it also means things like Java2HTML code blocks go missing too. So I had a look into alternatives. I started by trying to change the XSL sheet, I know only a little of XSL, and the task was either too much for me, or it was not so easy. So anyway, I gave up on that one. I thought about changing JSPWiki, but didn't like that idea because of the hassle of upgrading. Then I found this page:

http://www.re.be/css2xslfo/

It converts an XHTML page to PDF using a CSS file! (using the css2fop part)

So I started to modify your servlet to use it Pål (great work, thanks!). It doesn't do any explicit XML processing now, I used the "path of least resistance" and used a utility class provided by css2fop.

Prerequisits#

  • ApacheFOP (avalon-framework.jar, batik.jar, fop.jar)
  • jTidy (Tidy.jar)
  • JIMI - image inclusion in PDFs (JimiProClasses.jar)
  • CSS2FOP (css2fop1_2.jar or newer)
  • CSS2FOP version of Wiki2PDF plugin (wiki2pdf_css2fop.jar) - in attached wiki2pdf_css2fop.zip
  • CSS base files - (pdf_xhtml.css, pdf_xhtml_print.css) - in attached wiki2pdf_css2fop.zip
  • CSS sample file - (pdf.css) - sample in attached wiki2pdf_css2fop.zip

Installation#

  • Follow the installation notes for Pål's servlet above.
  • Copy all jar files listed above into your WEB-INF/lib directory.
  • Copy the base css files to your template dir (without these, your PDF will be empty)
  • Copy sample css file to your template dir
  • Restart your servlet container

Customisation#

Page titles: In my company, we use the wiki page name in our template as our page titles, but these are not included in the actual wiki text, so I needed to add them explicitly to the HTML sent for PDF generation. To turn off these page titles, add &title=false to the URL (e.g. http://host/wiki/wiki.pdf?title=false&page=PageName). When generated, the title is in a div block with class "title".

CSS stylesheet: To change the name of the CSS stylesheet to use for PDF generation, include &css=stylesheet.css to the URL (e.g. http://host/wiki/wiki.pdf?css=stylesheet.css&page=PageName). Remember that you need to import (or otherwise include the text of) the base CSS files - otherwise you will get an empty PDF. The default is pdf.css. These are kept in your template dir.

When creating custom CSS files, you should be aware that there is not a 1:1 translation between CSS and PDF (via XSLFO). Most things work, but some things do not. Your milage may vary.

The included base CSS files are very basic. Complex tables may not look too hot when converted to PDF because FOP does not support auto-generated width tables. If you manage to make some decent, professional CSS files, I'd be very interested in seeing them! Please share them here.

Update 2005-11-16 17:05 GMT:

  • Cleaned up a few things
  • Now allows preformatted text and Java2HTML plugin to work (not trivial!)
  • Supplied sample nice-ish looking CSS focused for JSPWiki

-- KieronWilkinson

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
zip
wiki2pdf_css2fop.zip 13.0 kB 1 14-Jun-2007 11:29 PålBrattberg
« This page (revision-4) was last changed on 22-Aug-2007 13:41 by Mantsch Thomas