Friday, March 28, 2008

Instructions for CF + iText + PdfPageEventHelper Example (Festivus for the Rest of Us)

Now it is time for the airing of grievances..

Perhaps I should be flattered, but I dislike articles that assume I know everything there is to know about the topic at hand. Let us face it, if I were omniscient I would not be would be reading the article in the first place ;) So for people like me, here are some detailed instructions to accompany the previous entry on using the PdfPageEventHelper.

For those that hate detailed instructions, well .. I am still airing my grievances. Wait your turn ;)

What you will need for the example:

1. Eclipse. Yes, you could use another IDE. However, since I am using Eclipse, you are on your own for instructions ;-)

2. Download the iText-2.0.7.jar (or newer) available at sourceforge.net

Example: My iText jar is located in the same directory as my CFM script. You can use a different location if you prefer.
C:\ColdFusionMX7\wwwroot\iTextExamples\iText-2.0.7.jar

3. Download and install the JavaLoader.cfc available at riaforge.org

Example: I installed the entire javaloader folder under the webroot
C:\ColdFusionMX7\wwwroot\javaloader


Step-by-Step Instructions



Step 1: Create the java jar that will be used from ColdFusion

A. Create a java project
  • Open Eclipse > File > New Java Project

  • Enter the name: iTextUtilities

  • Click "Finish"

B. Add the iText jar to the project
  • Project > Properties > Java Build Path

  • Select "Add External Jars" and navigate to wherever you saved the iText jar and select it.

  • Click "OK"

C. Create a package
  • Right click the "src" folder > New Package

  • Enter the name: itextutil

  • Click "Finish"

D. Create the java class
  • Right click the "itextutil" package > New Class

  • Enter the Name: MyPageEvent

  • Enter the Superclass: com.lowagie.text.pdf.PdfPageEventHelper

  • Click "Finish"

  • Paste in the java code

  • Compile the class: Project > Clean

  • Warning: The package and class names are case sensitive. The instructions/example may not work if you change the case.

E. Create the java jar
Step 2: Run the ColdFusion example

A) Install the jar files
  • Place the jar you created in step 1 AND the newer iText jar in a directory accessible to ColdFusion

  • Example: My jars are located here
    C:\ColdFusionMX7\wwwroot\iTextExamples\itextutil.jar
    C:\ColdFusionMX7\wwwroot\iTextExamples\iText-2.0.7.jar

B) Add the jars to the javaLoader

Example: I instantiated the JavaLoader in OnApplicationStart.


<!---
Application.cfc
--->
<cfcomponent>
<cfset this.name = "iTextExamples">
<cfset this.Sessionmanagement = true>
<cfset this.loginstorage = "session">
<!---
... other settings
---->

<cffunction name="onApplicationStart">
<!--- note, this is actually a harcoded UUID value --->
<cfset MyUniqueKeyForJavaLoader = "xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx">

<!--- if we have not already created the javaLoader --->
<cfif NOT structKeyExists(server, MyUniqueKeyForJavaLoader)>
<!--- construct an array containing the full path to the jars you wish to load --->
<cfset pathArray = arrayNew(1)>
<cfset arrayAppend(pathArray, expandPath('/iTextExamples/iText-2.0.7.jar'))>
<cfset arrayAppend(pathArray, expandPath('/iTextExamples/itextUtil.jar'))>

<cflock scope="server" type="exclusive" timeout="10">
<!--- verify again the javaloader was not already created --->
<cfif NOT StructKeyExists(server, MyUniqueKeyForJavaLoader)>
<cfset server[MyUniqueKeyForJavaLoader] = createObject("component", "javaloader.JavaLoader").init(pathArray)>
</cfif>
</cflock>
</cfif>
</cffunction>

<!---
... other functions
---->
</cfcomponent>


You should now be ready run the ColdFusion example

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep