Tuesday, February 24, 2009

Extract Coldfusion Cfchart as jpg or png image

While working on a reporting project recently, there was requirement of extracting the charts developed using CFCHART tag to a .jpg file. Coldfusion 8 has the ability where you can generate the graph as Binary data and then save it in .jpg or .png format.

Here is what needs to be done

1. use the "name" variable in cfchart and type as jpg or png.
<cfchart type="jpg" name="whatever">
--- Chart stuff here ---
</cfchart>

2. Then do the following, write the entire content to the file 

<cfset savedFile = getTempFile(getTempDirectory(),"Download") & ".jpg">
<cfset fileWrite(savedFile, whatever)>

3. Force the header for download

<cfheader name="Content-Disposition" value="attachment;filename=download.jpg">
<cfcontent type="image/jpeg" file="#savedFile#">

Thats ALL :-)