Create dynamic PDFs on the fly, and email them too

Create dynamic PDF?s on the fly and automatically send them as attachments.

There are a bunch of people out there that claim to have this elusive process figured out, or to have a wonder-tag that does it all for you. Well, I tried a lot of them, and the results were always less than desirable, if you want to call them results. However, through this trial and error, I was able to take the good points of some of these failed attempts, and piece them together into a working solution. 

Right off the bat, this is a solution for those of you who have the ability to install software on your own hosting server or, those of you who can convince your web host to install a little harmless application for you.


First, you'll need to download and install HTMLDOC (that ?harmless? application I just mentioned) at: http://www.easysw.com/htmldoc/

There is a free version that should work fine for your needs, it works for mine. This program works in conjunction with the next thing you'll need to download and install... the <CF_HTML2PDF3> custom tag, which you can get from the Macromedia developer's exchange...just search for CF_HTML2PDF3. This is a free download.

Next you'll want to create the file that makes the pdf...in this case I called it pdfDoc.cfm 
It's basically all of the normal html formatting, wrapped in the <CF_HTML2PDF3> custom tag.

That's your PDF, now what you do with it is up to you. In the case of this tutorial, we?re dealing with emailing it as an attachment to users who ask for an online Quote or Receipt. This is accomplished with a file called send_pdf.cfm 
The code for both is included in the zip file with this tutorial. As is this document.

I left out some things on the code to try to make it easier to follow, but all of the necessary code for converting the doc to pdf is here.

I hope that gets you started in the right direction.

<!---------- Here's the code for pdfDoc.cfm -------------------->
<!--- This template is used to display the actual quote in a
nice detailed view with pictures. --->


<!--- Get List of Cart Items if a cart exists and checkout isDefined--->
<CFIF IsDefined("Cookie.CartID")> 
    <CFQUERY datasource=
"#request.app.dsn#" NAME="GetCartItems">
        SELECT partno,
                   descript,
                   memo,
                   price,
                   mainPic,
                   Item,
                   Quantity 
        FROM    tblInmast
        WHERE  fpartno = Item
                 AND Cart = '#Cookie.CartID#' 
    </cfquery>
</CFIF>


<!--- Get Customer info on logged-in user ---> 
<cfquery name="getCust" datasource="#request.app.dsn#">
    SELECT FirstName, LastName, Address, City, State, Zip, Phone, Company, Email
    FROM Contacts
    WHERE FirstName = '#SESSION.Auth.FirstName#'
</cfquery>
<!---//--- HTML2PDF3 opening tag to capture the dynamic data ---// --->
<CF_HTML2PDF3 myHTMLDOC="#myHTMLDOC#"
                       myOPTIONS=
"--portrait --fontsize 10pt --bodyfont typeface Arial --bottom 20 --left 10 --right 15 --top 20" 
                       mySTATPATH=
"C:\Inetpub\wwwroot\yourSite\pdf"
                       mySTATFILE=
"static.html"
                       myPDFPath=
"C:\Inetpub\wwwroot\yourSite\pdf">

<!--- This is where you'll want to set up the actual output to be displayed in the PDF, you wrap it all in the <CF_HTML2PDF3> custom tag--->
<table width="606" border="0" cellspacing="0" cellpadding="3" bgcolor="#FFFFFF">
    <tr> 
        <td width=
"55"><div align="center"><img src="../pics/logo.gif" width="50" height="29"></div></td>
        <td width=
"238"><font color="#333333" size="2" face="Arial, Helvetica, sans-serif"><strong>Your Quote</strong></font></td>
        <td width=
"116">&nbsp;</td>
        <td width=
"43">&nbsp;</td>
        <td width=
"154"><font color="#333333" size="1" face="Arial, Helvetica, sans-serif">Your Company Address<br>123 Your St.<br>AnyCity, STATE Zip<br>555.555.5555 </font></td>
    </tr>

    <tr height="1"
        <td colspan=
"5" align="left" width="612" height="1"><img src="../assets/pics/general/hrLine.gif" width="612" height="1"></td>
    </tr>
    <tr valign=
"top"
        <td width="55"><font color="#333333" size="1" face="Arial, Helvetica, sans-serif">Attention:</font></td>
        <td width=
"238"><cfoutput><font size="1" face="Arial, Helvetica, sans-serif" color="##333333">#getCust.FirstName#
                #getCust.LastName#<br>
                #getCust.Company#<br>
                #getCust.Address#<br>
                #getCust.City#, #getCust.State# #getCust.Zip#</font></cfoutput></td>
        <td width=
"116">&nbsp;</td>
        <td width=
"43"><font size="1" face="Arial, Helvetica, sans-serif" color="#333333">Phone:<br>
               
Email:<br>
                <br>
<!--- Date: ---></font></td>
        <td width=
"154"><cfoutput><font size="1" face="Arial, Helvetica, sans-serif" color="##333333">#getCust.Phone#<br>#getCust.Email#<br><br></font></cfoutput></td>
    </tr>
    <tr height=
"1"
        <td colspan=
"5" align="left" width="612" height="1"><img src="../pics/hrLine.gif" width="612" height="1"></td>
    </tr>
    <tr> 

        <td colspan="5"><font color="#333333" size="1" face="Arial, Helvetica, sans-serif">Mission Statement: A blurb about your company might look nice here...don't you think?<br><br>We are pleased to provide you with the quotation below.<br><br></font></td>
    </tr>
    <tr> 
<td colspan=
"5" width="606">
   
         <table width="580" border="0" cellspacing="1" cellpadding="3" bgcolor="#333333">
                <tr bgcolor=
"#FFFFFF" align="center"
                    <td bgcolor=
"#cccccc" width="75"><font size="1" face="Arial, Helvetica, sans-serif" color="#333333"><strong>Item #</strong></font></td>
                    <td bgcolor=
"#cccccc" width="380"><font size="1" face="Arial, Helvetica, sans-serif" color="#333333"><strong>Description 
</strong></font></td>
                    <td bgcolor=
"#cccccc" width="81"><font size="1" face="Arial, Helvetica, sans-serif" color="#333333"><strong>Picture 
</strong></font></td>
                    <td bgcolor=
"#cccccc" width="44"><font size="1" face="Arial, Helvetica, sans-serif" color="#333333"><strong>Quantity</strong></font></td>
                </tr>

                <CFIF IsDefined("Cookie.CartID")>
                <cfoutput query=
"GetCartItems"
                <tr bgcolor="##FFFFFF"
                    <td align=
"center" width="75"><font size="1" face="Arial, Helvetica, sans-serif" color="##333333">#GetCartItems.partno#</font></td>
                    <td width=
"390"><font size="1" face="Arial, Helvetica, sans-serif" color="##333333"
                   
<CFIF GetCartItems.memo is "">
                       
#GetCartItems.descript# 
                    <CFELSEIF GetCartItems.memo is
"" AND GetCartItems.descript is "">
                       
No Description Available 
                    <CFELSE>
                       
#GetCartItems.memo#
                    </CFIF>
</font></td>
                    <td align=
"center" width="81"><CF_IMG src="../assets/pics/products/#mainPic#" MAXWIDTH="75" MAXHEIGHT="75"></td>
                    <td align=
"center" width="44"><font size="1" face="Arial, Helvetica, sans-serif" color="##333333">#GetCartItems.Quantity#</font></td>
                </tr>

                </cfoutput> 
                <cfelse>
                <tr>
                    <td colspan=
"4" bgcolor="#FFFFFF" width="606"><font size="1" face="Arial, Helvetica, sans-serif" color="#333333">Your Quote is Currently Empty</font></td>
                </tr>

                </CFIF>
            </table></td>
        </tr>
</TABLE>



<!--- //--- Close the HTML2PDF3 tag ---// --->
</CF_HTML2PDF3>

<!---------- Here's the code for send_pdf.cfm --------------------->
<!--- Get List of Cart Items if a cart exists and checkout isDefined--->

<CFIF IsDefined("Cookie.CartID")> 
    <CFQUERY datasource=
"#request.app.dsn#" NAME="GetCartItems">
        SELECT partno, descript, memo, price, mainPic, Item, Quantity 
        FROM tblInmast
        WHERE fpartno = Item AND Cart = '#Cookie.CartID#' 
    </cfquery>
</CFIF>

<!--- Get Customer info on logged-in user ---> 
<cfquery name="getCust" datasource="#request.app.dsn#">
    SELECT FirstName, LastName, Address, City, State, Zip, Phone, Company, Email
    FROM Contacts
    WHERE FirstName = '#SESSION.Auth.FirstName#'
</cfquery>

<!--- Response text to be displayed after user submits cart --->
<p><font color="#333333" size="2" face="Arial, Helvetica, sans-serif"><br><strong>
Thank you for using our cart. We'll contact you shortly.</strong><br>
</font> </p>

<!--- mail to person who submitted cart... this sends a normal html version and an attached PDF to the user's email address --->
<!--- the "mimeattach" attribute is what allows you to attach a PDF version of the quote to the email you're sending --->

<cfinclude template="pdfOutput.cfm">

<CFMAIL FROM="you@yourURL.com"
            To=
"#getCust.Email#"
            TYPE=
"HTML"
            SUBJECT=
"Thank you for your business"
            mimeattach=
"C:\Inetpub\wwwroot\yourSite\pdf\#Client.CFID#_#Client.CFToken#.pdf">
<Center>------ Here is a copy of your QuoteBuilder submission for your records ------</Center><br>
<center><cfinclude template="htmlOutput.cfm"></center>
</CFMAIL>

<!--- Delete items in the cart --->
<cfinclude template="deleteCart.cfm">

-------------------------------------------------------------------

NOTE:
You can also download the entire files, by clicking here - Pablo
-------------------------------------------------------------------



All ColdFusion Tutorials By Author: Ryan BeMiller
  • Create dynamic PDFs on the fly, and email them too
    Create dynamic PDF's on the fly and automatically send them as attachments. There are a bunch of people out there that claim to have this elusive process figured out, or to have a wonder-tag that does it all for you. Well, I tried a lot of them, and the results were always less than desirable, if you want to call them results. However, through this trial and error, I was able to take the good points of some of these failed attempts, and piece them together into a working solution. Right off the bat, this is a solution for those of you who have the ability to install software on your own hosting server or, those of you who can convince your web host to install a little harmless application for you.
    Author: Ryan BeMiller
    Views: 26,421
    Posted Date: Sunday, May 25, 2003
  • Display random images each time a page loads or is refreshed
    This tutorial will show you how easy it is to display an unlimited number of images randomly on your pages. Each time a page is loaded or refreshed a random image will load.
    Author: Ryan BeMiller
    Views: 21,123
    Posted Date: Tuesday, April 22, 2003
  • Slick Little Tell-A-Friend Feature
    Add this one page tell-a-friend file to your website and begin letting your visitors promote your site for you. Very easy to set up. Can be cfincluded anywhere on your site. Visitors just fill in their friends email address and click the send button. The rest is taken care of. Simple one click functionality for your visitors. Automatically sends an email, containing anything you wish, to your visitors friend.
    Author: Ryan BeMiller
    Views: 11,314
    Posted Date: Thursday, January 13, 2005
  • The Easiest Method for "Previous | Next" Recordset Navigation (aka - Recordset Paging)
    This tutorial provides a simple and quick way for CFers to add Recordset Navigation to their websites. This is the easiest way I could come up with to accomplish the "Previous Page | Next Page" kind of navigation that is needed by so many, yet understood by so few.
    Author: Ryan BeMiller
    Views: 17,192
    Posted Date: Wednesday, September 15, 2004