• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

CFajaxproxy and sending content in CallbackHandler as PDF - it it possible?

Is it possible to do the following (CF8, ajax and cfdocument as PDF)? And if possible, any how to suggestions are appreciated!

The web user clicks on a link which fires a cfajax http post to a cfc.  The cfc creates a pdf document using cfdocument.  I have a working model for this.  Using cfdebug and the CF Ajax Logger window, I can see the content being sent back is a PDF ("info:http: CFC invocation response: %PDF-1.4").

In the past I have used a cfc to create the PDF as a physical file in a temp location and sent the HTML link to a div on the template.  The end user then clicks the link to download the file.

What I would like to do INSTEAD is push the pdf content back to the user, and have an alert to "open or save the file".  In the cfc I have the cfdocument creation of the pdf (which, as noted, works) and then:

<!--- Make the generated pdf open as a separate file (not a cfm file) --->
<cfheader name="Content-Disposition" value="attachment;filename=#variables.filename#">
<!--- generate the pdf and send to the subscriber --->
<cfcontent type="application/pdf" reset="true" variable="#toBinary(thispdf)#" />

On the client side I have the javascript:

function getpdf(){
      var instance = new pdfconn();
function UpdateResponse(response){
      //populate cfwindow?  push the pdf to the user

Is this doable?
1 Solution
i am not sure one can display pdf files in a cfwindow, but try this:

in your cfc function save the generated pdf in a session scope var, then in the callback handler create/show cfwindow and point it to a .cfm page which will read that session var and display the pdf...

You want to use cfheader and cfcontent to stream the PDF to the browser, instead of sending the link.  Take a look at the top solution here:

Also you may want to look at other options in the cfheader.  For instance instead of attachment you can specify inline.  Attachment will try and open the save dialog, inline will try and stream it to the browser (although depending on browsers and plugins, I don't think you can guarantee what will happen)
akibashoAuthor Commented:
This comment pointed me in a direction that gave the solution:  I used the cfc to set a variable identifying the records to use to produce the pdf (I wasn't able to save the pdf into a session var, and realized that wasn't necessary or desirable).  

Then, once the cfc was called and the session set, I launched a regular window (not a cfwindow) with a window.open command to a CF template.  In that template I did the query with the saved session var (list of ids) as an argument and created the pdf using cfdocument, then cfheader and cfcontent to serve the pdf.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now