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

About This Tutorial
Author: Ryan BeMiller
Skill Level: Advanced 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 50,097
Submission Date: May 25, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • I would like to know how to install this custom add-on, it does not mention how to do so. I can install a .DLL custom tag but uhuh with this. I got own testing server, CFMX, Windows XP, Apache. Also i tried the test on the website and it does not cater for css, but thats just a note to css fans. Thankyou

  • This tag works great to output dynamic cfm pages, except it lacks the ability to output images and form elements when converted to a PDF....just a forewarning for anyone looking to use this tag to just that.

  • You're probably correct that it will not work with CSS. I think HTMLDoc is HTML specific...hence its name. As with anything, there are much more robust solutions available. But they aren't near as easy as this method, and they certainly aren't free. Good luck to you.

  • Works fine except if you use css positioning. HTMLDoc refuses to generate documents unless you have basic html taqs. Or at least I could not get it to work.

  • Dear Tom King Please, in my java program I generated pdf file on the fly and it reseived ColdFusion .cfm file, using getMessage function in java program. XmlToPdf class registration as customTag text is: #valSome# On display created frame of pdf with empty lists (5), and on the frame "opening" write message -- "... and Mozilla does not know how to handle this file type". This file save also in my java program on my folder on the disk for control and it is normal pdf file. Created PDF file on the fly is wrong.When I generated pdf as ByteArray in java program, in .cfm I reseived error " can not transformed ByteArray to String (I use function ToBase64() in ColdFusion --- , where getMess() get ByteArray. I compare wrong(fly) and rigth pdf(disk) and its different on second line 11 columns . I using UTF-8 or ISO-8859-1 charset, but not help. In java program is right, but when .cfm received generated pdf string , it consist wrong bit. %PDF-1.4 %БЦос -- second line (rigth) and %PDF-1.4 %^Q&>A ---- second line wrong file When I received ByteArray in logs files(administrator) I reseived error "Can not transver ByteArray to String. What I must do to received binary array in Coldfusion .cfm What I can do for reseiving pdf file on the fly, how I can correct my program or where I can find answer on my question. Thank You for advance Regards, Susanna Harutyunyan

  • Dear Sir I generated in my java program pdf as ByteArray and will create dynamic PDF’s on the fly in my .cfm program. I read this array with get method (from my java program, return byte []) byte [] cc=new byte[9000]; public void setMessage(byte [] msg){ cc=msg; } public byte [] getMessage(){ return this.cc; } In my .cfm program #valSome# Class XmlToPdf registration as customTag. When my .cfm forking, on display create the frame of pdf but empty text, number of list point(5) and this file put in the /tmp directory(is wrong). When I compare this wrong file on /tmp with normal, on 2 line in 11 columns different(1 byte or 1 bit??) Please, how I can dynamic PDF’s on the fly from my program Thank You for advance Regards, Susanna Harutyunyan

  • You're correct Brad. It is important to think about what you want/need to do with the PDF's after 'you are done with it'. I don't go into that in this tutorial, b/c that's a subject which could use a tutorial of its own. I'm assuming that once people figure out how to created these PDF's, they'll then take the next logical step and figure out how to manage them. I personally track them with a DB, then use a combination of CF and batch commands to get rid of files I no longer need, but again, that's a whole other tutorial.

  • So what happens to the PDF after you are done with it? If you try to delete it, you need to be careful that it happens after the mail takes place, or after it is displayed to the user. You could end up with lots of temp PDF files someplace clogging up your server.

  • Michael Shreiber, Credits are implied. I'm assuming anyone who knows how to follow a url or download a cf tag will undoubtedly figure out who these two elements are developed by. You'll certainly find no reference in my tutorial as to me taking credit for these elements. I trust the smart users here at easycfm.com know what is what. Thanks for your concern though.

  • Everytime I run the code I get a CFID element is undefined in the client? help.

Advertisement

Sponsored By...
Powered By...