July 9, 2004

PDF Encryption with CFMX & FOP

Sometimes it is necessary to encrypt pdf documents considering the information that is contained within them.

Apart of the FOP has built in support for PDF Encryption, by tweaking nateweiss’s FOP.cfc and doing a little configuration work things go pretty smoothly.

To set up CF for the encryption
1. Download the latest binary of FOP from apache.
2. Download the Java Cryptography Extensions (JCE) library extension from bouncy castle.
3. To the add java.security file in C:\CFusionMX\runtime\jre\lib\security the following line to the list of providers
security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider
4. For a standalone installation place the following files in the C:\CFusionMX\runtime\lib
   - fop.jar
   - Avalon-framework-cvs-20020806.jar
   - batik.jar
   - jce-jdk13-124.jar
5. In the CF administrator, under Java and JVM Settings add “C:\CFusionMX\runtime\lib\jce-jdk13-124.jar,C:\CFusionMX\runtime\lib\fop.jar,” to the class path
6. Restart ColdFusion MX Application Server Service

The EncryptedFOP.cfc:

<cfcomponent>
<!--- Initialize the FOP driver from xml.apache.org --->
<cfset THIS.fopdriver = CreateObject("java", "org.apache.fop.apps.Driver")>
<cfset THIS.lockname = CreateUUID()>

<cffunction name="ConvertFileToPDF" access="public">
<cfargument name="foFile" type="string" required="true">
<cfargument name="pdfFile" type="string" required="true">
<cfargument name="userPassword" type="string" required="true">
<cfargument name="ownerPassword" type="string" required="true">

<!--- Set up Java input source --->
<cfset var input = CreateObject("java", "org.xml.sax.InputSource")>
<cfset input.init( ARGUMENTS.foFile)>

<!--- Proceed with PDF generation --->
<cfset private_generateEncryptedPDF(input, pdfFile, userPassword, ownerPassword)>
</cffunction>
<cffunction name="ConvertStringToPDF" access="public">
<cfargument name="foString" type="string" required="true">
<cfargument name="pdfFile" type="string" required="true">
<cfargument name="userPassword" type="string" required="true">
<cfargument name="ownerPassword" type="string" required="true">
<!--- Set up Java input source --->
<cfset var reader = CreateObject("java", "java.io.StringReader")>
<cfset var input = CreateObject("java", "org.xml.sax.InputSource")>
<cfset reader.init(foString)>
<cfset input.init(reader)>

<!--- Proceed with PDF generation --->
<cfset private_generateEncryptedPDF(input, pdfFile, userPassword, ownerPassword)>
</cffunction>

<cffunction name="private_generateEncryptedPDF" access="private">
<cfargument name="input" type="any" required="true">
<cfargument name="pdfFile" type="string" required="true">
<cfargument name="userPassword" type="string" required="true">
<cfargument name="ownerPassword" type="string" required="true">

<!--- Output stream for writing PDF file --->
<cfset var output = CreateObject("java", "java.io.FileOutputStream")>

<!--- Rendering options for encrypting PDF file --->
<cfset var rendererOptions = CreateObject("java","java.util.HashMap")>

<!--- Get ready to do the PDF generation --->
<cflock name="#THIS.lockname#" type="exclusive" timeout="10">
<cflock name="#ARGUMENTS.pdfFile#" type="exclusive" timeout="10">
<!--- Turn on the output stream --->
<cfset output.init( ARGUMENTS.pdfFile )>

<!--- Set the rendering options --->
<cfset rendererOptions.put("ownerPassword", "#ownerPassword#")>
<cfset rendererOptions.put("userPassword", "#userPassword#")>
<cfset rendererOptions.put("allowCopyContent", "FALSE")>
<cfset rendererOptions.put("allowEditContent", "FALSE")>
<cfset rendererOptions.put("allowPrint", "FALSE")>
<cfset rendererOptions.put("allowEditAnnonations", "False")>

<!--- Hook the FOP driver to the input/output --->
<cfset THIS.fopDriver.setInputSource(input)>
<cfset THIS.fopDriver.setOutputStream(output)>
<cfset THIS.fopDriver.setRenderer(THIS.fopDriver.RENDER_PDF)>
<cfset THIS.fopDriver.getRenderer().setOptions(rendererOptions)>
<!--- Perform the actual PDF generation --->
<cfset THIS.fopDriver.run()>
<!--- Turn off the output stream --->
<cfset output.close()>
</cflock>
</cflock>

</cffunction>

</cfcomponent>

To Invoke the CFC

<cfinvoke component="EncryptedFOP" method="ConvertStringToPDF" foString="#TransformedXML#" pdfFile="#pdfFile#" userPassword="remotePass" ownerPassword="ownPass" >
Posted by Elyse at July 9, 2004 7:22 AM | TrackBack
Comments
Post a comment









Remember personal info?