Skip to main content
Participating Frequently
March 10, 2010
Question

how to call a CF File from another CF File

  • March 10, 2010
  • 2 replies
  • 1048 views

Hi

I am trying to call a CF1 File from another CF2 File.

CF1 File - takes some parameters which will help in generating PDF.

i need to call that CF1 File with parameters from another CF2 File. i cannot use hyperlink, i cannot use cflocation, i cannot use cfform tag, i cannot use any submit buttons or anything. just when i run CF2 File. CF1 File should trigger and the pdf output should be generated.  pdf output will only be generated in cf1 file only when i send some parameters to that file from CF2 File.

Thanks

Ravi

This topic has been closed for replies.

2 replies

Inspiring
March 15, 2010

cfinclude

existdissolve
Inspiring
March 10, 2010

What about invoking a cfc?

Participating Frequently
March 10, 2010

can you give me example code.

existdissolve
Inspiring
March 10, 2010

This is CERTAINLY not the most elegant solution, but it shows the intention, at least.

First, on the calling page, we have the following CFC invocation (it assumes a folder structure of root/com/excel.cfc):

<cfinvoke component="com.excel" method="makePDF" firstname="Douglas" lastname="Adams" />

The "component" attribute tells CF where to look for the cfc file and the "method" attribute tells CF which "method" to run once it finds the cfc.  Next, I simply pass in a couple arguments: firstname and lastname in this example.  Of course, these could just as easily be FORM or URL variables (or any other kind, for that matter.

Next, here's the CFC:

<cfcomponent>
    <cffunction name="makePDF" access="public" output="yes">
        <cfargument name="firstname" required="yes">
        <cfargument name="lastname" required="yes">
        <cfsavecontent variable="pdfdoc">
            #arguments.firstname# #arguments.lastname#
        </cfsavecontent>
        <cfheader name="content-disposition" value="attachment; filename=mypdf.pdf">
        <cfdocument format="pdf">
            <cfoutput>#pdfdoc#</cfoutput>
        </cfdocument>
    </cffunction>
</cfcomponent>

You'll notice at the top I have the arguments expected for this method specified--the same two that I passed from the calling page.  Next, I simply create the document content.

The most important part is the <cfheader>--by setting the "value" to what it is, it will force a download of the pdf, rather than trying to load it in the same page.  Finally, I just output the document content between <cfdocument> tags with a format of "pdf" which will take care of the pdf generation.

Again, not the most elegant solution, but I think it will work.  Hope it helps.