<

<cfdocument> has no body error

Published on
3,964 Points
964 Views
Last Modified:
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code.

The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a couple of header and footer sections. Also it was using a database query to loop over the records and display inside the cfdocument code to create a PDF file on the fly and display it in the browser.

This code shows what it was doing, I searched for this Issue but could not a find a answer so i tried different ways to tackle and found one very good solution.
 

<cfdocument format="PDF" localURL="yes">
	<cfoutput query="myquery">
		<!--- Section 1 --->
		<cfdocumentsection name="Section1-#myquery.id#">
		    <cfdocumentitem type="header">
		    <h1>This is a Header</h1>
		    </cfdocumentitem>
                    <table align="center"><tr><td>counter my query</td></tr></table>    
		    <cfdocumentitem type="footer">
		    <h1>Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</h1>
		    </cfdocumentitem>
		</cfdocumentsection>
		<cfdocumentsection name="Section2-#myquery.id#">
		    <cfdocumentitem type="header">
		    <h1>2nd header</h1>
		    </cfdocumentitem>
		    <table align="center"><tr><td>counter my query 2</td></tr></table>   
		    <cfdocumentitem type="footer">
		    <h1>Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</h1>
		    </cfdocumentitem>
		</cfdocumentsection>
	</cfoutput>
</cfdocument>

Open in new window

When this code is run, it will render perfectly, But if we have a scenario where your cfoutput query="myquery" is empty and returning no results, we will get the cfdocument has no body error.

So to solve this problem, we can do two things:

First approach:
 
<cfdocument format="PDF" localURL="yes">
       <cfif myquery.recordcount>	
       <cfoutput query="myquery">
		<!--- Section 1 --->
		<cfdocumentsection name="Section1-#myquery.id#">
		    <cfdocumentitem type="header">
		    <h1>This is a Header</h1>
		    </cfdocumentitem>
                    <table align="center"><tr><td>counter my query</td></tr></table>    
		    <cfdocumentitem type="footer">
		    <h1>Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</h1>
		    </cfdocumentitem>
		</cfdocumentsection>
		<cfdocumentsection name="Section2-#myquery.id#">
		    <cfdocumentitem type="header">
		    <h1>2nd header</h1>
		    </cfdocumentitem>
		    <table align="center"><tr><td>counter my query 2</td></tr></table>   
		    <cfdocumentitem type="footer">
		    <h1>Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</h1>
		    </cfdocumentitem>
		</cfdocumentsection>
	</cfoutput>
        <cfelse>
           <br>
        </cfif> 
</cfdocument>

Open in new window

This will render PDF and create a blank PDF Page because the query is returning null records and that goes into the clause where a tag is used. This will tell cfdocument that it has some contents, so it will not break and it will show it as an empty PDF. This is still vague, so we use another approach.

Second approach:
 
<cfif myquery.recordcount>
<cfdocument format="PDF" localURL="yes">	
       <cfoutput query="myquery">
		<!--- Section 1 --->
		<cfdocumentsection name="Section1-#myquery.id#">
		    <cfdocumentitem type="header">
		    <h1>This is a Header</h1>
		    </cfdocumentitem>
                    <table align="center"><tr><td>counter my query</td></tr></table>    
		    <cfdocumentitem type="footer">
		    <h1>Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</h1>
		    </cfdocumentitem>
		</cfdocumentsection>
		<cfdocumentsection name="Section2-#myquery.id#">
		    <cfdocumentitem type="header">
		    <h1>2nd header</h1>
		    </cfdocumentitem>
		    <table align="center"><tr><td>counter my query 2</td></tr></table>   
		    <cfdocumentitem type="footer">
		    <h1>Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</h1>
		    </cfdocumentitem>
		</cfdocumentsection>
	</cfoutput>
</cfdocument>
<cfelse>
<div align="center">Query is Empty, So PDF cannot be Generated.
</cfif>

Open in new window


Here, we are checking the database first. If there are any records, we create the PDF, but if there are no records, we go to the clause that shows the message to the user that the Query is Empty, So PDF cannot be Generated.
0
Comment
0 Comments

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Join & Write a Comment

Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
Please check the video also in regards to recovery of deleted emails from office 365 admin center and through the MFCMAPI tool. I have mentioned each and every step with the proper steps that need to be taken care of.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month