This blog post probably only applies to people building reusable components and UDFs, but I ran into an interesting issue today I thought I'd share with my readers. MrBuzzy (he prefers to go by his nickname) reported an issue with pdfUtils when used on a site with cfsetting enablecfoutputonly=true turned on.
This tag is one of the ways in which you can help reduce the amount of whitespace ColdFusion generates. I talk about this in depth here: ColdFusion Whitespace Options.
My pdfUtils CFC has a method, getText, that extracts the text from a PDF document. This method uses DDX (XML) to create the instructions necessary to get the text:
<!--- Create DDX --->
<cfsavecontent variable="ddx">
<?xml version="1.0" encoding="UTF-8"?>
<DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
<DocumentText result="Out1">
<PDF source="doc1"/>
</DocumentText>
</DDX>
</cfsavecontent>
<cfset ddx = trim(ddx)>
I had output="false" on the CFC method, but that doesn't prevent cfsavecontent from working. It just prevents any output from leaving the method. However, this immediately failed when used in context with cfsetting:
<cfsetting enablecfoutputonly="true">
<cfset pdf = createObject("component", "pdfutils")>
<cfset mypdf = expandPath("./testpdf.pdf")>
<cfset results = pdf.getText(mypdf)>
<cfdump var="#results#">
Of course, the fix was easy enough, I just wrapped the DDX in cfoutput:
<!--- Create DDX --->
<cfsavecontent variable="ddx">
<cfoutput>
<?xml version="1.0" encoding="UTF-8"?>
<DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
<DocumentText result="Out1">
<PDF source="doc1"/>
</DocumentText>
</DDX>
</cfoutput>
</cfsavecontent>
Simple enough of a fix, but I definitely will have to keep this in mind for my other CFCs.
Archived Comments
Ray, that's a very interesting point. I am a huge fan of CFSaveContent and have never thought about this side-effect. Part of me thinks it's silly to have to put CFOutput around things that don't get evaluated, but part of me things that when you package something for 3rd party use, you want to make it fool-proof.
What are you thinking? Are you gonna start adding this? Or, just wait till it crops up again?
I will do my best to add it to the list of things to check - like var scoping. Most likely I will forget - and find my own blog post via Google 2 years down the road. ;)
The worst is to come up with something that you think would make a great blog post idea; so, you get all excited about it and Google it to find out more information, only to find out that you already blogged about it two years ago :)
(I've been guilty of that several times)