Create Files on the Fly in ColdFusion
 
Published: 02 Nov 2006
Abstract
In this article Sarita examines how to create files in Excel, Word, and PDF format with the help of ColdFusion.
by Sarita Mishra
Feedback
Average Rating: 
Views (Total / Last 10 Days): 33756/ 60

Introduction

Developing an application in ColdFusion sometimes requires populating a desired file format, like PDF, XLS, HTML, DOC, XML, etc., from a ColdFusion page.  Depending on the requirement of the application, it is possible to create these file formats using certain predefined tags in ColdFusion.

Creating PDF files

To populate a PDF file we need to have some requirements in our system, such as Coldfusion or Adobe Acrobat full version.

These systems requirement some steps to be followed to populate PDF files from ColdFusion.

Create a PDF document to populate

Create a document using either Microsoft Word or html.  Adobe Acrobat must be there to create a PDF for that document.  Save it in WebServer .

Insert required fields using Acrobat

Open the PDF in Acrobat and insert the fields using the menubar Tools, like Form Tool, to create text fields.  Then give a name to each field, a font size and a style.  The name of each field corresponds to the data given as an input. Use the Text Tool to edit the text fields of fonts, size, etc.  Save it and place it in the WebServer.

Create a ColdFusion page to populate PDF

Now open any text editor and create a document; save it as .cfm extension.  Create a coldfusion form to collect data from the fields over internet or using database query.  This page will act as the way to populate the PDF file.

Below we have the code for a FDF file.FDF (Form Data File) which contains all the data to be merged with PDF.

Listing 1     

(The CFM codes for Input)
<FORM ACTION="output.cfm" METHOD="POST" ENABLECAB="YES">
      <P><B>First Name</B>
      <INPUT TYPE="TEXT" NAME="fname"></P>
      <P><B>Last Name</B>
      <INPUT TYPE="TEXT" NAME="lname"></P>
      <P><INPUT TYPE="SUBMIT" VALUE="Submit"></P>
</FORM>
(The Output Codes)

 <CFSETTING ENABLECFOUTPUTONLY="YES" SHOWDEBUGOUTPUT="NO">
 <CFCONTENT TYPE="APPLICATION/vnd.fdf">
 <CFOUTPUT>
%FDF-1.2                            --------- Header of FDF 
 1 0 obj <<
 /FDF <<
 /Fields                  -----------FDF body
 [
 <<
 /T(date)
 /V(#date#)
 >>
 <<
 /T(fname)
 /V(#form.fname#)
 >>
 <<
 /T(lname)
 /V(#form.lname#)
 >>
 ]
 /F(http://www.yoursite.com/pdf/file_name.pdf)      ------- The path of the PDF file
 >>
 >>
 endobj
 trailer -------- FDF footer   
 <</Root 1 0 R>>
 %%EOF
</CFOUTPUT>

Run the file and enter the required field to see the PDF file given in /F tag.

Creating Excel files

Steps

1) Create a .CFM page in ColdFusion and include some HTML codes.  Then the browser will load those html codes as an Excel spreadsheet.

Example: Creating Excel Sheet using HTML tag

Listing 2

<CFHEADER NAME="Content-Disposition" VALUE="inline; FILENAME=test.xls">
<CFCONTENT TYPE="application/vnd.msexcel">
 <table border="2">
  <tr>
      <td>Month</td>
                        <td>Quantity</td>
                        <td>$ Sales</td>
  </tr>
  <tr>
      <td>January</td>
       <td>80</td>
       <td >$245</td>
  </tr> 
</table>                                   

The above html table will be treated as an Excel worksheet and will be shown to the user in a different browser, instead of in the same application.  The "Content-Disposition" value="inline tag does this work.

Note: We can use CFQUERY tag to retrieve data from database and save them as an .xls file as well.

Example: Creating Excel Sheet using Query Result

Listing 3

<!--- Get Admin info. --->
<cfquery name="GetAdmins" datasource="testReport">
SELECT * FROM tb_company_admin
</cfquery>
<!--- Set content type. --->
<CFHEADER NAME="Content-Disposition" VALUE="attachment; FILENAME=admin.xls">
<CFCONTENT TYPE="application/vnd.msexcel">
<cfoutput>
<table bgcolor="blue"cols=4 rows=#Getadmins.recordcount# border="2">
<tr
      <td>ID</td>
<td>REG ID</td>
<td>USER NAME</td>
</font>
      </tr>
<cfloop query="Getadmins">
       <tr>
                <td>#admin_ID#</td>
                <td>#reg_id#</td>
                <td >#username#</td>
       </tr>
      </cfloop>
     </table>
 </cfoutput>                           

Example: Creating Excel Sheet using  coldfusion <cftable> tag for the above query

Listing 4

<cftable query = "Getadmins"startRow = "1" colSpacing = "3"
 color="oxffffff" HTMLTable colheaders>
              <!--- Each cfcol tag sets the width of a column in the table,
              the header information, and the text/CFML for the cell. --->
              <cfcol header = "<b>Admin ID</b>"
              align = "Left"
              width = 2
              text = "#admin_ID#">
              <cfcol header = "<b>Comapny ID</b>"
              align = "Left"
              width = 15
              text = "#reg_id#">
              <cfcol header = "<b>User Name</b>"
              align = "Center"
              width = 15
              text = "#username#">
            </cftable>                              
Creating MS Word file

Steps

1) The change in the type of the file to be created in CFCONTENT TYPE tag will solve this problem. Here it is msword type.

<CFHEADER NAME="Content-Disposition" VALUE="INLINE; FILENAME=filename.doc">
<CFCONTENT TYPE="application/msword" FILE="c:\temp\Cable.doc" DELETEFILE="yes"> 

Deletefile=”yes” will delete the file from the directory after sending it to the client.  If the user wants to be prompted with an option to save the file or open it in the browser then the value="inline will solve this.  You can add all types of style setting which generally we do in html.

Listing 5

<CFHEADER NAME="Content-Disposition" VALUE="INLINE;
FILENAME=test.doc">
 <CFCONTENT TYPE="application/msword">
 <CFOUTPUT>
 <html>
 <head>
 <style type='text/css'>
 p,td,ul,li {font-family: arial; helvetica; font-size: 12px}
 h1 {font-family: arial; helvetica; font-size: 16px;}
 </style>
 </head>
 <body>
 <h1><font color="blue">This is an example of creating .doc files
 from coldfusion.</font></h1>
 <p>
 <font style="Times New Roman">
 This text is only meant for the purpose of
 user creating .doc files from within    coldfusion .cfm page.
 </font>
 </p>
 </body>
 </html>
 </CFOUTPUT>
Creating .XML file

Steps

1) The change in the type of the file to be created in CFCONTENT TYPE tag will solve this problem. Here it is XML type.

<CFHEADER NAME="Content-Disposition" VALUE="INLINE; FILENAME=filename.xml">
<CFCONTENT TYPE="application/XML"  DELETEFILE="yes">

Deletefile=”yes” will delete the files from the directory after sending it to the client.  If a user wants to be prompted with an option to save the file or open it in the browser then the value="inline will solve this.

Listing 6

  <CFHEADER NAME="Content-Disposition" VALUE="INLINE; FILENAME=test.xml">
  <CFCONTENT TYPE="application/XML"  DELETEFILE="yes"> 
       <Book>
       <Author>Abraham Linkon</Author>
       <Price>Rupees 500</Price>
       </Book>

This will create an .xml file which can be opened in a browser and can be used independently in subsequent ColdFusion pages as ordinary .xml files.

Conclusion

Creating files in a required format in Coldfusion pages is now an easy job.  There is no need to export the file as the required format.  This is one of the more important aspects of Coldfusion from a developer's point of view.  Suggestions and feedbacks are always welcome to enhance the knowledge regarding this article.

 



User Comments

Title: great   
Name: Sandy
Date: 2006-11-02 6:33:11 AM
Comment:
Great job.. keep moving ahead
Title: Kar   
Name: Chandrakanta
Date: 2006-11-01 2:56:09 AM
Comment:
It is very nice and can also helpful for the new developers. Come across creating different file formats at the time of requirement.
Title: Great Job   
Name: pd
Date: 2006-11-01 2:41:58 AM
Comment:
Keep it up.
Title: Nice Article   
Name: Tom Anderson
Date: 2006-10-31 1:46:13 AM
Comment:
Very Nice and informative article for people working on ColdFusion.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-18 5:26:21 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search