Using Wrap() in a PDF

This post is more than 2 years old.

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>

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#">

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

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

Archived Comments

Comment 1 by Ben posted on 4/9/2009 at 1:19 AM

Or along the same lines, could you use ParagraphFormat()?

Comment 2 by Raymond Camden posted on 4/9/2009 at 1:40 AM

A person on the thread mentioned that. I tried it - but the paragraphFormat removes the newlines wrap creates.

Comment 3 by Aaron West posted on 4/9/2009 at 2:39 AM

Ray, what's up with your code view missing all the coloring and what not? I've just now noticed it though it may have been like this for some time now. Are you running your code view through ColdFish like recent BlogCFC versions?

Comment 4 by Thilo Hermann posted on 4/9/2009 at 11:07 AM

You could also use wrap and white-space:pre (css) which IMHO is more flexible than HTML <pre>

<div style="white-space:pre;">#wrap(mytext,40)#</div>

Comment 5 by Thilo posted on 4/9/2009 at 11:11 AM

Oops, sorry! Just looked it up. white-space:pre is working perfectly in HTML but is not supported by cfdocument :(

Comment 6 by Raymond Camden posted on 4/9/2009 at 3:29 PM

@Aaron: My blog here is -way- old, circa 5.8 or something. In theory it wouldn't be hard to drop in ColdFish, I've just never gotten around to it. I want to deploy a new UI this year sometime, and I figured at that time I'd update to the latest BlogCFC.

Comment 7 by Vikas Patel posted on 8/23/2011 at 9:48 AM

In second method you are adding HTML break tag, but what if I am using HTML's pre tag? browser's css will not work here. Is there any suggestion for this case?

Comment 8 by Vikas Patel posted on 8/23/2011 at 10:03 AM

Never mind, worked perfectly...