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:
- Download Cocoon 2.15
- Build and install Cocoon
- Create or download the source code files to build the examples in this article (the XML, XMAP, XSL, and JSP files)
- 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.
The following procedure lists the steps to build and deploy Cocoon.
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.
Next, build the war file using the command build war.
|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/).|
|Author's note: These are separate commands. Perform both build, and build war.
- 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.
- 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.