<cfdocument> has no body error

Coast LineCEO
CERTIFIED EXPERT
Published:
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
1,695 Views
Coast LineCEO
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.