RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Publishing XML Documents in PDF and HTML with Cocoon

Sure, you can create a custom framework for multi-format publishing, but why bother? It's already been done. Take advantage of Apache Cocoon's ability to deliver PDF and HTML documents on demand.


his article shows you how to publish XML documents in PDF using Apache Cocoon. Cocoon can create result documents in a variety of formats, including HTML and PDF. Cocoon's popularity reflects that of the Adobe's PDF format, which most vendors prefer for e-mailing richly formatted printable documents and reports. Cocoon converts not only text content, but also graphs and images. This article focuses on using Cocoon to dynamically generate HTML and PDF.

Although there are alternatives for generating PDF, Cocoon is preferred because it provides various value-added features. For example, the Cocoon site says: "Cocoon interacts with many data sources, including file systems, RDBMS, LDAP, native XML databases, SAP systems, and network-based data sources. It adapts content delivery to the capabilities of different devices like HTML, WML, PDF, SVG, and RTF, to name just a few. You can run Cocoon as a Servlet as well as through a powerful, command line interface. The deliberate design of its abstract environment gives you the freedom to extend its functionality to meet your special needs in a highly modular fashion." You can find a complete feature list at http://cocoon.apache.org/2.1/features.html.

What You Need
To gain the greatest benefit from this article, you should be somewhat familiar with XML/XSL and J2EE. You can use any Java Web application server; I used the JBoss 3.2.1 server to test the article code.

Follow these steps to get started:

  1. Download Cocoon 2.15
  2. Build and install Cocoon
  3. Create or download the source code files to build the examples in this article (the XML, XMAP, XSL, and JSP files)
  4. Test the application
The rest of this article describes how to perform each of the steps in more detail.

Download the Cocoon Source
To get Cocoon and configure it for your server, first download the latest version from http://Cocoon.apache.org/2.1/index.html.

Building Cocoon
The following procedure lists the steps to build and deploy Cocoon.

  1. Execute build (default target)—just type build on the command prompt in the directory where you installed Cocoon (by default that's C:\Java\Cocoon-2.1.5.
    Figure 1. The Cocoon Welcome Page: After building and installing Cocoon as described in this article, you should see this page by browsing to the default Welcome page (http://localhost:8080/cocoon/).
  3. Next, build the war file using the command build war.
Author's note: These are separate commands. Perform both build, and build war.

  1. Deploy the Cocoon Web application war file by copying the Cocoon.war file generated by the build process in step 2 from \Cocoon-2.1.5\build\Cocoon-2.1.5\Cocoon.war to the default\deploy directory. When you complete these steps Cocoon should be up and running on JBoss.
  2. Test your Cocoon installation by starting the JBoss server and go to http://localhost:8080/cocoon/. If the installation succeeded, you'll see the screen shown in Figure 1.
Author's Note: The information in this article assumes that you're running your local HTTP server on port 8080.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date