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

2 comments:

Nhoel said...

can i do it to cf7?
please do email me for the answer since i cant monitor this thread or please email me if you answer the question with the link. thank you very much.

Brijesh said...

You should be able to do it with ColdFusion 7, CF 7 supports cfchart output as binary data, so it would work