Using the FOP CFC

Hi. This page is for people who saw my presentation on XML at the 2002 Macromedia DevCon conference and would like to play with the ability to create PDF documents via XML and XSLT. You'll need to download and install FOP to get the PDF generation working. FOP is an open source renderer of XSL-FO documents. FOP does a number of things, but its main purpose in life is to accept XSL-FO code and turn it into a PDF document. If you're not familiar with XSL-FO, just think of it as being like HTML, but for printable document formats (like PDF) instead of web pages.

In any case, here's what you need to do.

  1. Go to xml.apache.org/fop and download FOP. I have only used and tested the 0.20.4 version (dated July 8th), so I recommend you start with that. Download the smaller "bin" file unless you're curious about looking at the Java source code for FOP, in which case get the "src" version. In any case, you'll only actually need a few files from the download.
  2. Put the fop.jar file from the download somewhere (I put it in CFusionMX\runtime\jre\lib).
  3. Also put the avalon-framework JAR and the batik.jar in the same place.
  4. Now go to the CF Admin and add the full path to the fop.jar (including the c:\ and the fop.jar parts themselves) to the JVM classpath. You don't need to add the locations of the other two JARs.
  5. Restart CF and the examples (from my 2002 DevCon presentation, available at nateweiss.com) should work.

For purposes of this discussion, the file you are most interested in here is the ExampleApp\FOP.cfc file from my presentation examples. It's a ColdFusion Component (CFC) that is essentially just a wrapper around the CF code needed to talk to FOP using its Java API. It makes FOP easy to use in CF code.You can either use the CFC as-is, or you can study the guts of FOP.cfc to learn how to create your own code for talking to FOP.

Currently, the CFC exposes two methods:

In either case, the first argument is the XSL-FO input (which you can provide as a string or a filename on the server's drive) and the second argument is the path to write the completed PDF file to. The input is most likely the result of an XSLT transformation that you perform with CFMX's XmlTransform() function. The output would most likely then be sent to the browser using <CFCONTENT> (possibly using DeleteFile="Yes" so the file is deleted after the user gets it). Example code is in the ExampleApp\Transformer.cfc file from my DevCon examples.

The CFC takes care of simple locking and other safeguards internally. You are encouraged to take a look the FOP.cfc file to see how it works, learning a bit about CFC's along the way.

 

nate