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
Finally passed the test
Managing in light of McGregor's Theory X and Theory Y
CMMI
Kicking HIT Leadership Up a Notch
That's just some mumbo jumbo project management BS
Outcomes - The tactic to get to the strategy
Nurse Call, VOIP, and Wi-Fi: Its just cool when things come together!
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
August 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
November 2005
October 2005
September 2005
August 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
Joel on Software
David Ross
Edward Prevost
Martin Fowler
The Health Care Blog
The Tales of Hoffman
The Business Word
Medical Rants
Christina's Considerations
Paul Levy
HIS Talk
Appropriate IT
Candid CIO
RSS feed




