Using Wrap() in a PDF

I ran into an interesting question today on Adobe’s ColdFusion Forums. How would one use wrapped text in a PDF generated by CFDOCUMENT? It was obvious that the user wasn’t really aware of what wrap did. His first test was a simple check in the browser:

<!--- Text from Flatland, (Edwin A. Abbott 1838-1926) ---> <cfsavecontent variable="sampletext"> But now, drawing back to the edge of the table, gradually lower your eye (thus bringing yourself more and more into the condition of the inhabitants of Flatland), and you will find the penny becoming more and more oval to your view; and at last when you have placed your eye exactly on the edge of the table (so that you are, as it were, actually a Flatlander) the penny will then have ceased to appear oval at all, and will have become, so far as you can see, a straight line.

The same thing would happen if you were to treat in the same way a Triangle, or Square, or any other figure cut out of pasteboard. As soon as you look at it with your eye on the edge on the table, you will find that it ceases to appear to you a figure, and that it becomes in appearance a straight line. Take for example an equilateral Triangle - who represents with us a Tradesman of the respectable class. Fig. 1 represents the Tradesman as you would see him while you were bending over him from above; figs. 2 and 3 represent the Tradesman, as you would see him if your eye were close to the level, or all but on the level of the table; and if your eye were quite on the level of the table (and that is how we see him in Flatland) you would see nothing but a straight line. </cfsavecontent>

<cfset newText = wrap(trim(sampleText),40)>

<cfoutput> #newText# </cfoutput> </code>

Running this will give you one big block of text in the browser. Viewing source though shows the proper wrap. Wrap is not really meant for HTML viewing, but rather emails, or other fixed content type strings.

You can wrap the output in PRE tags and that will work, both in the browser and in PDF, but that gives you slightly ugly text as well. An alternative fix would be to replace the line breaks with BRs:

<cfset newText = replace(newText, chr(10), "<br/>", "all")>

Here is a complete template showing this in action:

<!--- Text from Flatland, (Edwin A. Abbott 1838-1926) ---> <cfsavecontent variable="sampletext"> But now, drawing back to the edge of the table, gradually lower your eye (thus bringing yourself more and more into the condition of the inhabitants of Flatland), and you will find the penny becoming more and more oval to your view; and at last when you have placed your eye exactly on the edge of the table (so that you are, as it were, actually a Flatlander) the penny will then have ceased to appear oval at all, and will have become, so far as you can see, a straight line.

The same thing would happen if you were to treat in the same way a Triangle, or Square, or any other figure cut out of pasteboard. As soon as you look at it with your eye on the edge on the table, you will find that it ceases to appear to you a figure, and that it becomes in appearance a straight line. Take for example an equilateral Triangle - who represents with us a Tradesman of the respectable class. Fig. 1 represents the Tradesman as you would see him while you were bending over him from above; figs. 2 and 3 represent the Tradesman, as you would see him if your eye were close to the level, or all but on the level of the table; and if your eye were quite on the level of the table (and that is how we see him in Flatland) you would see nothing but a straight line. </cfsavecontent>

<cfset newText = wrap(trim(sampleText),40)> <cfset newText = replace(newText, chr(10), “<br/>”, “all”)>

<cfdocument format=”pdf” name=”data”> <cfoutput> #newText# </cfoutput> </cfdocument>

<cfheader name=”Content-Disposition” value=”inline; filename=test.pdf”> <cfcontent type=”application/pdf” reset=”true” variable=”#data#”> </code>

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments