Skip to main content
Participant
July 31, 2009
Answered

Conditionally Apply cfdocument tag

  • July 31, 2009
  • 2 replies
  • 803 views

I would like to display a .cfm as HTML or as a pdf, controlled by a POST variable.  I had considered having a cfm for the html (let's call it view.cfm), then have a view_pdf.cfm that merely had a <cfdocument format="pdf" src="view.cfm"> tag.  However, this did not work--I assume that that is because the src attribute only accepts html documents.  I then tried something like the following:

<cfif checkforvariablehere >
<cfdocument format="pdf" > </cfif> OUTPUT STUFF HERE <cfif checkforvariablehere >

</cfdocument> </cfif>

However, this gave me problems with the closing cfdocument tag, which is sensible since the tags overlap.  The only way I have found is to have a cfif/cfelse structure, with the output in cfdocument tags in the first and without cfdocument tags in the second.  This is, of course, stupid, since now any change I make to output I have to make in two places.  Is this the only solution available to me, or is there something else I can use?

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    You may have better luck with cfsavecontent.  Use it to generate whatever you want displayed, and then do this

    <cfif you want html>

    <html><head><body>

    <cfouput>#your variable#</cfoutput>

    <cfelse>

    <cfdocument>

    <cfouput>#your variable#</cfoutput>

    Plus all your closing tags of course.

    2 replies

    BKBK
    Community Expert
    Community Expert
    July 31, 2009
    I had considered having a cfm for the html (let's call it view.cfm), then have a view_pdf.cfm that merely had a <cfdocument format="pdf" src="view.cfm"> tag.

    I think you were on the right track. If you thought of the cfinclude tag, you would have got a neat solution similar to Dan's, for example

    view.cfm

    =======

    <!--- the html --->

    view_pdf.cfm

    ==========

    <cfdocument format="pdf">

    ...

    </cfdocument>

    testpage.cfm

    ===========

    <cfif checkforvariablehere>

    <cfinclude template="view.cfm">

    <cfelse>

    <cfinclude template="view_pdf.cfm">

    </cfif>

    One advantage here is that you separate the HTML content from the PDF.

    Dan_BracukCorrect answer
    Inspiring
    July 31, 2009

    You may have better luck with cfsavecontent.  Use it to generate whatever you want displayed, and then do this

    <cfif you want html>

    <html><head><body>

    <cfouput>#your variable#</cfoutput>

    <cfelse>

    <cfdocument>

    <cfouput>#your variable#</cfoutput>

    Plus all your closing tags of course.

    Participant
    July 31, 2009

    Thank you very much, I appreciate this.