<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

<cfdocument> has no body error

Published on
4,064 Points
1,064 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

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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…
This is Part-2 of Learning to use the Power of Mailwasher Pro so if you haven't watched Part-1 yet, I urge you to do so before watching this video. Click this link to watch Part-1 (https://www.experts-exchange.com/videos/56638/Learn-to-use-the-POWER…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month