Hack 91 Generate PDF Documents from XML and CSS

figs/moderate.gif figs/hack91.gif

Produce PDF documents for XML documents styled with CSS using YesLogic Prince.

YesLogic (http://www.yeslogic.com) of Melbourne, Australia, offers an extremely simple little tool for converting XML documents styled with CSS into PDF or PostScript. It's called Prince and it's currently at Version 3.0. It runs on Windows or Red Hat Linux (Versions 7.3 and 8.0). Prince comes with a set of examples, default stylesheets, and DTDs.

You can download a free demo version from http://yeslogic.com/prince/demo/. This demo is fully featured but outputs the word Demo in an outline font across every page it creates. If you like it, you can purchase a copy from http://yeslogic.com/prince/purchasing/.

This simple XML document represents a time:

<?xml version="1.0" encoding="UTF-8"?>

<!-- a time instant -->

<time timezone="PST">





 <atomic signal="true" symbol="&#x25D1;"/>


The CSS stylesheet provides detailed formatting information for it:

time {font-size:40pt; text-align: center }

time:before {content: "The time is now: "}

hour {font-family: sans-serif; color: gray}

hour:after {content: ":"; color: black}

minute {font-family: sans-serif; color: gray}

minute:after {content: ":"; color: black}

second {font-family: sans-serif; color: gray}

second:after {content: " "; color: black}

meridiem {font-variant: small-caps}

After downloading and installing Prince, open the application and follow these steps:

  1. Select Documents Add and then select the document time.xml from the directory of working examples.

  2. Select Stylesheets Add and select time.css from the same location.

  3. Select the Output menu and select PDF if it isn't already selected.

  4. Click Go and Prince produces a PDF based on time.xml combined with time.css. The application should look like Figure 6-19.

    Figure 6-19. YesLogic Prince after converting time.xml styled with time.css into time.pdf

  5. Select time.pdf in the lower-left pane and right-click it. If Adobe Reader is installed on your computer, you should be able to open time.pdf with it. If it is not installed, get a free copy from http://www.adobe.com/products/acrobat/readstep2.html.

Figure 6-20 shows you time.pdf in Adobe Reader Version 6.0.

Figure 6-20. time.pdf in Adobe Reader 6.0

?Michael Fitzgerald