Copy link to clipboard
Copied
I am using ColdFusion 11 and have just started using the EncodeForHTML function instead of HTMLEditFormat whenever I need to display text that the user has entered, usually via a textarea. We need to save the formatting so that information the user has entered remains understandable. Users have a choice of whether to display the info to the screen or export to a pdf. We are using EncodeForHTML to make the displayed info safer and we are using <pre> to retain the required formatting.
My problem is that when I need to export the data to a pdf, then EncodeForHTML acts differently than HTMLEditFormat does. For some reason, I get extra lines inserted.
Here's some sample code. Just save to a cfm and run:
-------------------------------------------------------
<cfsavecontent variable="f">This is my sample data.
Value 1 20%
Value 2 30%
Value 3 50%
Here's my summary of this data.
</cfsavecontent>
<cfhtmltopdf saveasname="MyTest.pdf">
<cfoutput>With EncodeForHTML: <pre>#encodeForHTML(f)#</pre></cfoutput>
<cfoutput>With HTMLEditFormat: <pre>#HTMLEditFormat(f)#</pre></cfoutput>
</cfhtmltopdf>
----------------------------------------------------------
The HTMLEditFormat version shows up correctly. Any idea how to work around this problem?
If you temporarily comment out the cfhtmltopdf tag and run the page, you will see the reason for the difference. The function encodeForHTML outputs extra encodings, like 
, 
 and so on.
One way to solve the problem is to use the canonicalize function, for example,
<cfhtmltopdf saveasname="MyTest.pdf">
<cfoutput>With EncodeForHTML: <pre>#canonicalize(encodeForHTML(f),false,false)#</pre>
With HTMLEditFormat: <pre>#HTMLEditFormat(f)#</pre></cfoutput>
</cfhtmltopdf>
Copy link to clipboard
Copied
If you temporarily comment out the cfhtmltopdf tag and run the page, you will see the reason for the difference. The function encodeForHTML outputs extra encodings, like 
, 
 and so on.
One way to solve the problem is to use the canonicalize function, for example,
<cfhtmltopdf saveasname="MyTest.pdf">
<cfoutput>With EncodeForHTML: <pre>#canonicalize(encodeForHTML(f),false,false)#</pre>
With HTMLEditFormat: <pre>#HTMLEditFormat(f)#</pre></cfoutput>
</cfhtmltopdf>
Copy link to clipboard
Copied
Combining functions can be overkill. Looking again at this, it could be simpler.
Copy link to clipboard
Copied
So...what would be the simpler way to do it? We just upgraded from CF8 to CF11, so I'm new to these functions and am just figuring out all their ramifications. I tried your canonicalize suggestion and it worked. However, if there's a simpler way, I'd like to try that before changing everything.
Copy link to clipboard
Copied
I would suggest you go with just one function, for example, the simpler one, HTMLEditFormat.
Copy link to clipboard
Copied
Except that according to ColdFusion Help | Deprecated Features HTMLEditFormat is deprecated. So that'll just be a short term solution. Thanks for your help though.