Ask a Jedi: Sending a dynamic PDF via email

Chas asked:

Thanks for your great series on working with PDFs. I have a question I hope you can help me with. I want to create a PDF (simple enough using cfdocument) but then I would like to email it without having to write it disk using a filename. I can create it and save to "name", but I can't seem to be able to use that in the cfmailparam. Any way to generate a PDF and email it on the fly?

Turns out this is rather simple, if you are using ColdFusion 8.0.1. CF801 added the ability to use cfmailparam to attach files stored in a variable. For example:

<cfdocument format="pdf" name="pdfdoc"> <h1>Snazzy PDF</h1>

Here is your cool PDF that is dynamic: <cfoutput>#randRange(1,100)#</cfoutput> </cfdocument>

<cfmail to=”ray@camdenfamily.com” from=”foo@foo.com” subject=”Your PDF”> <cfmailparam file=”doc.pdf” content=”#pdfdoc#”> Attached please find your cool PDF. Enjoy! </cfmail>

Done. </code>

The first part of the template creates a simple dynamic PDF. Then I mail it to myself. Note that cfmailparam uses both file and content attributes. The content will be the actual data attached to the email while the file attribute will be used for the file name.

In case you are wondering why this isn’t in the docs, or why I didn’t do this in my ‘emailing cfchart’ blog post, it is simply the fact that the main docs have not been updated for 8.0.1. You will be able to find this documented, along with everything else new, in the 8.0.1 release notes: http://www.adobe.com/support/documentation/en/coldfusion/releasenotes.html#801

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. You can even buy me a coffee!

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

Comments