Every now and then I can't find a good solution for a reader so I take to the "airways" and ask my good readers for help. This is one of those days. Rick emailed me a few days ago with an interesting issue.

He had a query that he used to create a set of PDFs. In each PDF, he included a bit of dynamic data, like name, age, etc. It worked, but something odd happened. Consider the following sample code.

<cfset getStudents = queryNew("fname,lname,email", "cf_sql_varchar,cf_sql_varchar,cf_sql_varchar", [
	{fname:"Ray",lname:"Camden",email:"raymondcamden@gmail.com"},
	{fname:"Joe",lname:"Blow",email:"jblow@gmail.com"},
	{fname:"Scott",lname:"Stroz",email:"boyzoid@gmail.com"}]
	)>


<cfoutput>
 
    <cfloop query="getStudents">
   
        <cfdocument format="pdf" orientation="landscape" name="certificate">

        	<p style="background-image:url(cert.jpg);background-repeat:no-repeat;height:680px;width:900px">
			<font size="+2">#fname# #lname#</font>
			</p>
			
        </cfdocument>
       
        <cfmail from="no_reply@monoc.org" to="#email#" subject="Your Course Completion Certificate" type="html">
       
	        <cfmailparam file="certificate.pdf" type="application/pdf" content="#certificate#" />
        </cfmail>
       
    </cfloop>
   
</cfoutput>

Pretty straightforward, right? Each PDF is stored in memory and then attached to an email. When executed, emails go out, the text is dynamic per the code specified, but only the first PDF has the background attachment as defined in CSS.

As an FYI, when you create emails with attachments, you can find the attachments pretty easily. Open the mail file first, and then make note of the file line. Here is a sample from one of my tests: file: /Applications/ColdFusion10/cfusion/runtime/work/Catalina/localhost/tmp/cftmp1249913661563960132.tmp

The file is named .tmp, but I found the file, renamed it .pdf, and was able to view it just fine.

Anyway, I did some digging into this and immediately found some interesting tips:

  • Use localurl="true"
  • Use a file:/// path

But none of these worked. I also tried adding a bit of randomness to the URL, thinking it was something in ColdFusion's request handling, but that didn't work either. I then made the variable used for PDF data dynamic. Why? Who the heck knows. I was trying everything. Here is the final version of the script, and one you can run yourself, to see the bug in action.

<cfset getStudents = queryNew("fname,lname,email", "cf_sql_varchar,cf_sql_varchar,cf_sql_varchar", [
	{fname:"Ray",lname:"Camden",email:"raymondcamden@gmail.com"},
	{fname:"Joe",lname:"Blow",email:"jblow@gmail.com"},
	{fname:"Scott",lname:"Stroz",email:"boyzoid@gmail.com"}]
	)>


<cfoutput>
 
    <cfloop query="getStudents">
   
        <cfdocument format="pdf" orientation="landscape" name="certificate#currentRow#" localurl="true">

        	<p style="background-image:url(cert.jpg);background-repeat:no-repeat;height:680px;width:900px">
			<font size="+2">#fname# #lname#</font>
			</p>
			<hr/>
			<p style="background-image:url(file:///Users/ray/Dropbox/websites/testingzone/cert.jpg);background-repeat:no-repeat;height:680px;width:900px">
			<font size="+2">#fname# #lname#</font>
			</p>
			<hr/>
			<p style="background-image:url(cert.jpg?x=#createUUID()#);background-repeat:no-repeat;height:680px;width:900px">
			<font size="+2">#fname# #lname#</font>
			</p>
			
        </cfdocument>
       
       <cfset content = variables["certificate#currentRow#"]>
        <cfmail from="no_reply@monoc.org" to="#email#" subject="Your Course Completion Certificate" type="html">
       
	        <cfmailparam file="certificate.pdf" type="application/pdf" content="#content#" />
        </cfmail>
       
    </cfloop>
   
</cfoutput>

So... any ideas?