cfdocument issue - cfdocument has no body - coldfusion

I am working with Coldfusion10 and am facing this error:
The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request
cfdocument tag has no body.
It must have a body or a source file or URL.
I checked the website and detected that cfsettings is not defined at the top or anywhere which can cause this issue, I am using it as
<cfdocument format="pdf">
<cfdocumentsection>
<cfdocumentitem type="header"></cfdocumentitem> - Footer is used too
</cfdocumentsection>
I tried using evalAtPrint= true but still no success. Am I missing something here?

Make sure you are actually putting in at the end. I'm assuming you just missed this here.
Otherwise everything seems to align up with the Wiki Docs.
I would suggest two things.
Verify you are using ColdFusion 11 Update 3. Update 3 was a major update and may have addressed this issue.
If you are using update 3, open a bug at bugbase.adobe.com

The error message you included in your question indicates that there is no content between your <cfdocument> tags. The code that you included corroborates that. If this is not your actual code then we cannot be of much help.
You need to include the content that you would like to be converted into PDF between the <cfdocument> tags. You need something like this:
<cfquery datasource="cfdocexamples" name="empSalary">
SELECT Emp_ID, firstname, lastname, e.dept_id, salary, d.dept_name
FROM employee e, departmt d
WHERE e.dept_id = d.dept_id
ORDER BY d.dept_name
</cfquery>
<cfdocument format="PDF">
<cfoutput query="empSalary" group="dept_id">
<cfdocumentsection>
<cfdocumentitem type="header">
<font size="-3"><i>Salary Report</i></font>
</cfdocumentitem>
<cfdocumentitem type="footer">
<font size="-3">Page #cfdocument.currentpagenumber#</font>
</cfdocumentitem>
<h2>#dept_name#</h2>
<table width="95%" border="2" cellspacing="2" cellpadding="2" >
<tr>
<th>Employee</th>
<th>Salary</th>
</tr>
<cfset deptTotal = 0 >
<!--- inner cfoutput --->
<cfoutput>
<tr>
<td>
<font size="-1">#empSalary.lastname#, #empSalary.firstname#</font>
</td>
<td align="right">
<font size="-1">#DollarFormat(empSalary.salary)#</font>
</td>
</tr>
<cfset deptTotal = deptTotal + empSalary.salary>
</cfoutput>
<tr>
<td align="right"><font size="-1">Total</font></td>
<td align="right"><font size="-1">#DollarFormat(deptTotal)#</font></td>
</tr>
<cfset deptTotal = 0>
</table>
</cfdocumentsection>
</cfoutput>
</cfdocument>
Copied from the ColdFusion documentation here

Related

Coldfusion creating reports with wkhtmltopdf

I am trying to get some assistance with wkhtmltopdf.
I have downloaded and installed and trying to get it to work correctly with my program.
What I have is a form that lets the user choose print, preview or excel. When the user chooses print I want pop up the html table that is created by a query depending on the associates and locations that are chosen.
Right now when the user chooses print it just shows this table in the browser.
<cfif FORM.Format IS "print">
<!---<cfdocument format="pdf" scale="75" backgroundvisible="yes" overwrite="no" fontembed="yes">--->
<link rel="stylesheet" href="css/form-table.css"/>
<!---<cfdocumentitem type="header" >
<cfoutput><p align= "right">Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</p></cfoutput>
</cfdocumentitem> --->
<div class="RTable">
<h3 class="RTable-h3">CHECKLIST STATS</h3>
<cfoutput>
<ul class="RTable-headingList">
<li>FROM <span class="RTable-headingList-date">#dateFormat(date1, 'mm/dd/yyyy')#</span> TO <span class="RTable-headingList-date">#dateFormat(date2, 'mm/dd/yyyy')#</span></li>
<li>LOCATIONS: <span class="RTable-headingList-locations">#locList#</span></li>
</ul>
</cfoutput>
<table class="table table-hover RTable-table">
<thead>
<tr>
<th>Associate Name</th>
<th>Location</th>
<th><small>Generated by</small>Associate</th>
<th><small>Generated by</small>Selected Location(s)</th>
<th><small>Associate Percentage of</small>Location Total</th>
</tr>
</thead>
<tbody>
<cfoutput query="GetEmployeeInfo">
<tr>
<td class="RTable-name"><cfif rnA EQ 1><strong>#assoc_name#</strong></cfif></td>
<td class="RTable-location"><cfif rnL EQ 1>#trans_location#</cfif></td>
<td>#checklistsByAssocLoc#</td>
<td>#assocChecklistsByLoc#</td>
<td>#DecimalFormat(totalChecklistsByAssocLocPct)# %</td>
<!---<td> rnA: #rnA# | rnL: #rnL# | rnTotAssoc: #rnTotAssoc# </td> --->
</tr>
<cfif rnTotAssoc EQ 1>
<tr class="RTable-row-associate-total">
<td>Associate Total</td>
<td></td>
<td>#totalChecklistsByAssoc#</td>
<td>#totalAssocChecklistsByAllFilteredLoc#</td>
<td>#DecimalFormat(totalChecklistsByLocPct)# %</td>
</tr>
</cfif>
</cfoutput>
</tbody>
</table>
</div>
<!---</cfdocument>--->
I am trying to use it like cfdocument in the <cfif FORM.Format IS "print"> do I cfexecute this table some how in replace of how I have the table? I am using this as reports and dont want to save a million reports to the server. I guess I am looking for some assistance in getting off on the right foot. Any help would be greatly appreciated.
Iv tried adding this code inside the if "print":
<cfexecute name="C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
arguments="http://path/path/checklist/wkhtmltestpage.cfm C:\temp\wkhtmlTest.pdf"
errorVariable="errorMessage"
timeout="10"
/>
But this does not download it to show the user to print.
Also for some reason its not going to the link I am specifying its making a pdf of the login page...
When WKHTMLTOPDF requests the webpage, the "user" is not authenticated and the CGI.Http_User_Agent contains "wkhtmltopdf". The remote IP of the request will also be a local IP that is configured on the server. As a result, I don't directly process any CFM scripts using WKHTMLTOPDF.
I recommend:
Use CFContent to capture generated static HTML.
Save the HTML to a public-accessible directory with a random file name.
Use the web path + random file name w/WKHTMLTOPDF command line.
After PDF is generated, delete random files.
This process will make any issues easier to troubleshoot. I prefer creating static BAT files with the same randomized filename so that I can manually re-run on the server or even locally on my on PC to further troubleshoot and view any weird messages returned by the program. There's many other command line settings you'll want to pass (margins, orientation, pagesize, header/footer, JS delay, etc), so creating a standalone BAT file with all of the command line arguments is the best approach. (I noticed in another question that you may be using CFX_Exec. I recommend using it over CFExecute.)
<!--- Simple WKHTMLTOPDF Generation Usage --->
<cfset FileID = CreateUUID()>
<cffile action="WRITE" file="#webroot#\#FileID#.htm" output="#TheHTML#">
<cfexecute name="C:\wkhtmltopdf.exe" arguments="http://mywebsite.com/#FileID#.htm C:\temp\#FileID#.pdf" timeout="30">
<cffile action="DELETE" file="#webroot#\#FileID#.htm">
To deliver the PDF file, you can either perform a 302 redirect to the randomized PDF file and let your webserver handle the mimetype (and then delete it later) or use CFContent to deliver it using a ColdFusion thread and automatically delete it:
<!--- Return PDF to browser inline or as downloadable attachment --->
<!--- <cfheader name="content-disposition" value="attachment; filename=""NiceFilename.pdf"""> --->
<cfheader name="content-disposition" value="inline; filename=""NiceFilename.pdf""">
<cfcontent type="application/pdf" file="C:\temp\#FileID#.pdf" deletefile="Yes">

Coldfusion app generating blank PDF page

We had a server migration and since the migration, a Coldfusion application is generating blank pdf pages that are able to be saved locally and printed. The application queries a SQL Server database. I have been through the code several times and am pretty positive the data sources are all pointed to the new environment. I have a report from the day before the migration and all was well. I think the paths are fine because when I changed the path, the application blew up. I have minimal experience with Coldfusion and I am out of ideas at the moment. What else should I be looking at when debugging this application? Has any code been deprecated and the new environment will not run it? Is it possible there are settings on the new server preventing this PDF from being generated? Do I need to add a MIME somewhere that was unnecessary before? Any advice would be helpful. Below is some of the code being used.
<cfif #qSelect.Record# EQ 0>
<h1>Empty recordset</h1>
<cfelse>
<cfdocument filename="../Reports/CustomReport.pdf" format = "pdf" orientation = "landscape" overwrite="yes">
<h2>Database</h2>
<cfoutput>
<h4>Monthly Report </h4>
</cfoutput>
<table cellpadding="3" cellspacing="0" border="0">
<tr>
<th>Date</th>
<th>Status</th>
</tr>
<cfoutput query="qLocalEvents">
<tr>
<td>#DateFormat(qSelectEvents.EventDate, "mm/dd/yyyy")#</td>
<td>#qSelectEvents.Status#</td>
</tr>
</cfoutput>
</table>
</cfdocument>
</cfif>

How can I leave the loop if no records are part of the if statement?

Below code works, but I would like to show 'no records' message if
<cfif GetResults2.csedept_id eq aFieldValue> has no records for that value. I have tried to put a counter on, but I just can't get it to show 'no records' and not show the
<thead> <th>Name</th> <th>Positive Comment</th> <th>Negative Comment</th></thead> heading of the table if there are no records.
How can I show "No Records" and hide the table head if the results come back empty
Right now if the result comes back empty it will say "no results"(correct) and display the header(incorrect).
<cfset counter3= 0>
<table cellpadding="0" cellspacing="0" class="tablecolors">
<h2> Comments </h2>
<thead> <th>Name</th> <th>Positive Comment</th> <th>Negative Comment</th></thead>
<cfloop query="GetResults2">
<cfif GetResults2.csedept_id eq aFieldValue>
<tr>
<td nowrap="nowrap">#emp_namefirst# #Left(emp_namelast, 1)# </td>
<td>#Replace(commentpositive, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
<td>#Replace(commentnegative, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
</tr>
<cfelse><p>no records</p>
</cfif>
</cfloop>
</table>
UPDATE: Just to add I do have another query above like #FRANK said that does pretty much the same things for example:
'<cfloop query="GetEmployeeTotals3">
<cfif GetEmployeeTotals3.csedept_id eq aFieldValue> '
here is the query:
select GetResults.* , GetEmployees.emp_namefirst, GetEmployees.emp_namelast
from GetResults, GetEmployees
where employee = emp_id
order by csedept_id
so all the solutions above that I tried won't work.
I would recommend using a query of queries to whittle the result set down to just the values you care about first. Then you can easily check, the recordcount before outputting the table at all.
Alternatively, loop first and build up the results with cfsavecontent, then check to see if you found any prior to getting to the table bits.
Something like this should work. You'll have to set a flag of showRecords that will determine if you should show the headers.
<cfset showRecords = false>
<cfloop query="GetResults2">
<cfif GetResults2.csedept_id eq aFieldValue>
<cfset showRecords = true>
<cfbreak>
</cfif>
</cfloop>
<h2> Comments </h2>
<cfif showRecords>
<table cellpadding="0" cellspacing="0" class="tablecolors">
<thead> <th>Name</th> <th>Positive Comment</th> <th>Negative Comment</th></thead>
<cfloop query="GetResults2">
<cfif GetResults2.csedept_id eq aFieldValue>
<tr>
<td nowrap="nowrap">#emp_namefirst# #Left(emp_namelast, 1)# </td>
<td>#Replace(commentpositive, emp_namefirst, "<B>" & emp_namefirst & "</B>")# </td>
<td>#Replace(commentnegative, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
</tr>
</cfif>
</cfloop>
</table>
<cfelse>
<p>no records</p>
</cfif>
EDIT: Updated answer after question was clarified
You'll need to move your cfif to include your table head when checking for records. If the check comes back with a record count, then output the table head as well as the results, otherwise output "no results". I've written two versions below, one is utilizing the cfoutput instead of a loop (just personal preference) and one with your loop if you wish to keep it.
I added Dan's listfind() valuelist() combo...so give him credit for that.
Edit Edit so this is the super-strength/ultra-correct awesome solution with the 'I-am-tired-of-editing-this-answer-50-times-to-get-OP-an-answer', with bonus shameless theft from Dan's answer above/below. Or where ever we ending up order-wise.
<table>
<cfif getresults2.recordcount AND ListFind(ValueList(GetResults2.csedept_id), aFieldValue)>
<tr>
<th>name</th>
<th>positive comment</th>
<th>negative comment</th>
</tr>
<cfoutput query="getresults2">
<tr>
<td nowrap="nowrap">#emp_namefirst# #left(emp_namelast, 1)# </td>
<td>#replace(commentpositive, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
<td>#replace(commentnegative, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
</tr>
</cfoutput>
<cfelse>
<tr colspan="3">
<td><p>no records</p></td>
</tr>
</cfif>
</table>
If you have your cfoutput somewhere in your code (where we cannot see), then here is your cfloop back, simply replace this where I have the cfoutput in the snippet above.
<cfloop query="getresults2">
<tr>
<td nowrap="nowrap">#emp_namefirst# #left(emp_namelast, 1)# </td>
<td>#replace(commentpositive, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
<td>#replace(commentnegative, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
</tr>
</cfloop>
While your question is still unclear, this might be what you want.
<cfif ListFind(ValueList(GetResults2.csedept_id), aFieldValue)>
your existing code to display results in a table
<cfelse>
<p>No Records Found</p>
Note that this:
ListFind(ValueList(GetResults2.csedept_id), aFieldValue)
will execute and return false even if GetResults2 has no records at all.
Edit starts here
Based on this comment, "well if it only appears on some then display does but to those who do not match then ignore" change your existing code to something like:
<cfquery name = "q3" dbtype = "query">
select * from GetResults2
where csedept_id = #aFieldValue#
<cfquery>
<table>
column header row
<cfoutput query="q3">
data rows
</cfoutput>
<table>
The initial construct at the start of this answer still applies.

CFDocument Images

I am having a problem displaying images within cfdocument. I have tried several different options without luck (please see test code below).
It seems that cfdocument recognizes there is an image file there, because the size of the table cell changes. Yet still it doesn't display content of the image.
I would appreciate any help.
<cfdocument format="PDF" localURL = "yes">
<p style="background-color:#006633">
<table border="1">
<tr><td><cfoutput>
output from "file:///#ExpandPath('CEUCertificate_ABondy3.jpg')#"
</cfoutput>
</td>
<td style="background-color:white">
<cfoutput>
<image src="file:///#ExpandPath('CEUCertificate_ABondy3.jpg')#">
</cfoutput>
</td>
</tr>
<tr><td><cfoutput> output from #ExpandPath('CEUCertificate_ABondy3.jpg')# </cfoutput> </td>
<td>
<cfoutput>
<image src="#ExpandPath('CEUCertificate_ABondy3.jpg')#">
</cfoutput>
</td>
</tr>
<tr><td style="background-color:red">
Output from: img src="CEUCertificate_ABondy3.jpg"
</td>
<td style="background-color:red">
<img src="CEUCertificate_ABondy3.jpg"/>
</td>
</tr>
<tr>
<td style="background-color:white">
output from image src="CEUCertificate_ABondy3.jpg"
</td>
<td>
<cfoutput>
<image src="CEUCertificate_ABondy3.jpg">
</cfoutput>
</tr>
<tr>
<td>Output from local url</td>
<td>
<cfoutput>
<img src=#localUrl("CEUCertificate_ABondy3.jpg")#>
</cfoutput>
</td>
</tr>
</table>
</p>
</cfdocument>
<cffunction name="localUrl" >
<cfargument name="file" />
<cfset var fpath = ExpandPath(file)>
<cfset var f="">
<cfset f = createObject("java", "java.io.File")>
<cfset f.init(fpath)>
<cfreturn f.toUrl().toString()>
</cffunction>
For your initial question, you need to reference the images in your cfdocument tag as you would with any other HTML page; <img src="url-to-the-image" />. So this should work for you.
<img src="/cde/mobileweb/CEUCertificate_ABondy3.jpg"/>
Now that you have made that change the page is timing out. This is probably due to how the image was created. See this page here. Which I found from Charlie's page here.
Try opening that image in an editor and re-saving it. Then see if the processing time decreases.
I found some more discussion about this on Ben Nadel's blog here.
Instead of messing with ExpandPath() and file://, code it as a normal html and make sure it works first. Then use localUrl="true" in your <cfdocument>

cfloop empty query condition?

I have the following ColdFusion code that is getting information from a database and displaying the results on the homepage. Here's the cfquery code:
<cfquery name="getSchedule" datasource="#APPLICATION.datasource#" dbtype="odbc">
SELECT * FROM SCHEDULE_Days SD
LEFT JOIN SCHEDULE_ScheduledClasses SSC ON SD.day_id = SSC.day_id
LEFT JOIN SCHEDULE_Classes SC ON SSC.class_id = SC.class_id
WHERE SD.day_date = #createODBCDate(now())# AND SSC.schedule_cancelled = 0
ORDER BY SSC.start_time
</cfquery>
and the output code:
<cfoutput>
<cfloop query="getSchedule">
<tr>
<td width="40"> </td>
<td width="74">#lcase(timeFormat(start_time,"h:mm tt"))#</td>
<td width="158">#class_name#</td>
</tr>
</cfloop>
</cfoutput>
The issue is, if there is no data contained within getSchedule (i.e., there are no ScheduledClasses), it displays nothing.
I'm looking for a way to change this so that, in the event that there is no data to display, I can specify a message and code to be shown in its absence.
First just a quick CF tip you can make you code better by doing it this way:
<cfif getSchedule.recordcount GT 0>
<cfoutput query="getSchedule">
<tr>
<td width="40"> </td>
<td width="74">#lcase(timeFormat(getSchedule.start_time,"h:mm tt"))#</td>
<td width="158">#getSchedule.class_name#</td>
</tr>
</cfoutput>
<cfelse>
<p>Empty record message here</p>
</cfif>
The reason I put the query output first is most likely this will happen more than with your empty set message.
<cfif getSchedule.recordcount>
.... do something
</cfif>
Will work just aswell there is no need for gt 0
Use the recordCount to detect whether the query has any record
<cfif getSchedule.recordcount gt 0>
.... do something
</cfif>
<cfif getSchedule.RecordCount>
<table>
<cfoutput query="getSchedule">
<tr>
<td width="40"> </td>
<td width="74">#lcase(timeFormat(start_time,"h:mm tt"))#</td>
<td width="158">#class_name#</td>
</tr>
</cfoutput>
</table>
<cfelse>
<p>There are currently no records</p>
</cfif>