XML

Converting an XSL-FO Document to PDF

The point of all this effort in learning about the XSL-FO language is to do something useful, such as generate a highly formatted PDF document suitable for viewing in Adobe's popular Acrobat Reader application. As I mentioned earlier in the lesson, a handy tool for carrying out the XSL-FO to PDF translation is FOP, which is a free command-line tool made available by the Apache XML Graphics Project at http://xmlgraphics.apache.org/fop/.

After downloading and installing FOP, you need to tweak a setting in a FOP batch file before attempting to run the application. This file is called fop.bat, and it contains a variable called LOCAL_FOP_HOME that you must set to the actual FOP installation folder. As an example, if you install FOP to the folder \fop-0.20.5, you'll need to change the line of code for the LOCAL_FOP_HOME variable in fop.bat to the following:

set LOCAL_FOP_HOME=c:\fop-0.20.5

The fop.bat batch file is used in Windows versions of FOP. The UNIX/Linux equivalent is fop.sh, which is the shell script for running FOP.

With the FOP batch file configured properly, you're ready to take FOP for a spin. The quickest way to use it is to just copy the .fo file that you want to process to the main FOP folder. Then open a command-line prompt, navigate to the main fop folder, and issue the following command:

fop fofile.fo pdffile.pdf

The first argument to FOP, fofile.fo, is the name of the XSL-FO file that you want to process, whereas the second argument is the name of the resulting PDF file. Following is how you would process the skeleton.fo document using FOP:

fop skeleton.fo skeleton.pdf

Figure 14.4 shows the resulting skeleton PDF document as viewed in Adobe Acrobat Reader.

Figure 14.4. The Skeleton PDF document as viewed in Adobe Acrobat Reader.

I realize the skeleton PDF document isn't much to get excited about but it is a full-blown PDF document that was generated straight from an XSL-FO document, which is no small feat. The remainder of the lesson guides you through a more complete XSL-FO example that results in the generation of a much more interesting PDF document.