So you may have noticed I didn't post a new CFPDF post last night. Problem was - well, I ran into a few problems. I'd like to describe one of them now as I'm sure other people will run into this as well.
Consider the following simple code:
<cfdocument format="pdf" name="mydocument">
<cfloop index="x" from="1" to="15">
<p>
lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
</p>
</cfloop>
</cfdocument>
<cfcontent type="application/pdf" reset="true" variable="#mydocument#">
This code takes some random text and simply feeds it to a cfdocument tag. Nothing special, right? But if I decide to manipulate the PDF? I'll be covering page deleting in full later on, but for now, look at this slightly modified version:
<cfdocument format="pdf" name="mydocument">
<cfloop index="x" from="1" to="15">
<p>
lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
</p>
</cfloop>
</cfdocument>
<cfpdf action="deletepages" pages="1" source="mydocument" name="mydocument">
<cfcontent type="application/pdf" reset="true" variable="#mydocument#">
This example simply takes the PDF, removes page one, and then serves it up again. But when you run this, you will get:
coldfusion.pdf.PDFDocWrapper is not a supported variable type. The variable is expected to contain binary data.
So this kind of makes sense I guess. My initial variable was binary data. When I used CFPDF to manipulate it, ColdFusion converted it into a PDF variable (like images, PDFs are a native data type in ColdFusion), and when I tried to use it as binary data in cfcontent, it complained. Now I think it would be nice if cfcontent would give me a hand here and just deal with it, but luckily there is an easy work around - toBinary:
<cfdocument format="pdf" name="mydocument">
<cfloop index="x" from="1" to="15">
<p>
lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
</p>
</cfloop>
</cfdocument>
<cfpdf action="deletepages" pages=1 source="mydocument" name="mydocument">
<cfcontent type="application/pdf" reset="true" variable="#toBinary(mydocument)#">
The only change in this last version was to wrap my mydocument variable in a toBinary call.
Make sense? Anyone else get tripped up by this?
Edit - I am ashamed to admit I forgot to thank the person who pointed this out to me - Greg Oberlag. This whole entry is based on what he taught me.
Archived Comments
I was about to start fooling around with the pdf tags until I read that you had planned on doing a series so I figured I would wait and see what you discovered.
Its likely now that I won't get tripped up with this at all :)
Thanks!
I guess what you're seeing happen makes sense since I'd assume CF would have to work with the PDF in some native data structure to be effective. That being said, you'd think the default output format would be binary when the PDF is asked to serialize itself. Cool find.
Your posts are like "Where's Waldo?" except instead, "Where's Paris?" Lol, great stuff.
Heh, I hope folks don't mind. I swear I'm not doing it for link bait. A few weeks ago I was working with a friend, got a bit stressed, and said something like, "I just can't concentrate. I'm too concerned about Paris." This was during her incarceration. It was funny - and now it is just sticking. I'm sure I'll switch back to Star Wars refs soon.
I haven't been tripped up by it yet, but I have run across similar issues in the past. Specifically, at one point I was storing PDFs as base-64 encoded strings in a DB (yeah, not efficient, but it was special circumstances)... I had to do a lot of toBinary() and cfcontent-ing to get it all to work.
Same general idea here...
I know this particular entry was about a problem you were having, but it got me started on fixing a problem I was having and I had to take a break to write this. Sometimes CF gets me so excited about solving a problem, that I practically have an anxiety attack getting started. And the options seem endless again. Does that ever happen to you?
Heh, I've spent more late nights working on cool (well to me) CF ideas than playing video games. ColdFusion - not just a language - a passion! ;)