CFDocument Images - coldfusion

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>

Related

cfdocument issue - cfdocument has no body

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

creating a custom td tr table for Displaying the Data

I have the following string and i want to split that string to display data in table format but the way i want to display is not working, Here is the data and here is how it should look like
vendorname- #name#: city-#city#: state-#state#:zip-#zip#:in network-#innetwork#
i want to create the above as the following table:
<table>
<tr>
<td>vendorname</td><td>#vendorname#</td>
<td>city</td><td>#city#</td>
<td>state</td><td>#state#</td>
</tr>
<tr><td>zip</td><td>#zip#</td>
<td> </td><td> </td>
<td> </td><td> </td>
</tr>
</table>
trying to create 6 columns in one TR
Here is try so far
<table align="center" width="100%" border="0" cellpadding="4" cellspacing="6" bordercolor="#CCCCCC;">
<tr>
<cfloop index="aPair" list="#Demo_Details#" delimiters=":">
<cfset Key= listFirst(aPair,"-")>
<cfif listLen(apair,"-") gt 1>
<cfset value= listLast(aPair,"-")>
<cfelse>
<cfset value = "">
</cfif>
<cfoutput>
<td><strong>#key#</strong></td>
<td>#value#</td>
</cfoutput>
</cfloop>
</tr>
</table>
It is not generating the columns as expected, it is showing everything in single line
How about something like this
<cfset Vendor = "">
<cfset City = "">
<cfset State = "">
<cfset ZIP = "">
<cfloop index="aPair" list="#Demo_Details#" delimiters=":">
<cfset Key= listFirst(aPair,"-")>
<cfif listLen(apair,"-") gt 1>
<cfset SetVariable(key, listLast(aPair,"-")>
</cfif>
</cfloop>
<cfoutput>
<table align="center" width="100%" border="0" cellpadding="4" cellspacing="6" bordercolor="#CCCCCC;">
<tr>
<td>vendorname</td><td>#vendorname#</td>
<td>city</td><td>#city#</td>
<td>state</td><td>#state#</td>
</tr>
<tr>
<td>zip</td><td>#zip#</td>
<td> </td><td> </td>
<td> </td><td> </td>
</tr>
</table>
</cfoutput>

CFDocumentItem - change font size

I am creating a footer in a PDF document using <cfdocumentitem type="footer"> and it is working fine. Except that I cannot seem to control the font. I have tried <span>, <font face> etc. with no luck. Currently I am trying a table with class as below.
Anyone know how to control the font look in a footer with <cfdocumentitem>?
<cfdocument format="pdf"
marginBottom = ".5"
marginLeft = ".4"
marginRight = ".4"
marginTop = ".2"
>
<style type="text/css">#import "pdf.css";</style>
<cfdocumentitem type="footer">
<cfoutput>
<table width=100%>
<tr>
<td class=verd10>
<b>#pdfstuff.pdffinal#</b>
</td>
<td align=right class=verd10 valign=top>
Page #cfdocument.currentPageNumber# of #cfdocument.currentPageNumber#
</td>
</tr>
</table>
</cfoutput>
</cfdocumentitem>
pdf document data etc
</cfdocument>
Try adding a copy of the stylesheet link inside the footer, like so:
<cfdocumentitem type="footer">
<style type="text/css">#import "pdf.css";</style>
<cfoutput>
<table>
... etc
</table>
</cfoutput>
</cfdocumentitem>
Welcome to cfdocument. The only way I have been able to successfully format any text in the footer is to use a combination of html and inline styles.
Here is a footer that works for me:
<cfdocumentItem type="footer">
<table width="100%">
<tr>
<td style="font-family:Arial; font-size: 9pt;">Printed On: #dateFormat(now(), "mm/dd/yyyy")# #timeFormat(now(), "HH:MM")#</td>
<td style="font-family:Arial; font-size: 9pt;" align="right">Page #cfdocument.currentPageNumber# of #cfdocument.totalPageCount#</td>
</tr>
</table>
</cfdocumentItem>

ColdFusion 9 ReReplace nth occurence of HTML tag

I have the following HTML stored in a variable in ColdFusion 9. I need to insert a new table row after the 4th </tr>. i.e. before the Submit button.
<form name="form1" id="form1" action="" method="post">
<table>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref1" name="ref1" value="1" > </td></tr>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref2" name="ref2" value="2" > </td></tr>
<tr>
<th style="text-align:left;">Name * </th>
<td><input type="text" name="foo" id="foo" size="30" maxlength="50" value=""></td>
</tr>
<tr>
<th title="Please enter plain text or HTML." style="cursor:help;text-align:left;">Comment * </th>
<td><textarea name="bar" id="bar" cols="40" rows="10" ></textarea></td>
</tr>
<tr>
<th colspan="1"></th>
<td>
<input style="width:80px" type="submit" value="Submit">
<input style="width:80px" type="button" value="Cancel">
</td>
</tr>
</table>
ReReplace seems like the way to go, but I'm having trouble getting the regexp right. Another option would be to split the string and rebuild it with my new HTML in the middle. Any suggestions would be appreciated.
Regex is the wrong tool for this - you want a HTML parser.
Here's how you can do it with JSoup:
<cfsavecontent variable="InputHtml">
[insert code from question]
</cfsavecontent>
<cfsavecontent variable="NewRow">
<tr><th>whatever</th><td>stuff</td></tr>
</cfsavecontent>
<!--- Read "Creating Objects From Jar Files" --->
<cfset jsoup = createObject('java','org.jsoup.Jsoup') />
<cfset HtmlDom = jsoup.parse(InputHtml) />
<cfset HtmlDom.select('tr:eq(4)').after( NewRow ) />
<cfoutput>
<pre>#XmlFormat(HtmlDom.body().html())#</pre>
</cfoutput>
You can see details of what selectors are supported in the JSoup Selector API
If you don't know/care how many lines are in the table, you can do...
HtmlDom.select('table>tbody').append( NewRow )
...to just add the new row at the end.
Creating Objects From Jar Files
The above code most likely wont work instantly if you copy and paste it, because your server doesn't know about JSoup - you need to download the Jar file and put it in a sensible location.
For CF9, you need to copy the jsoup-1.6.3.jar into your {coldfusion}/lib directory then restart the server.
For CF10, you can use this.JavaSettings in your Application.cfc (as described here) to place it in a different location.
For Railo and OpenBD, you can specify the location of the JAR file as a third argument, e.g:
<cfset jsoup = createObject('java','org.jsoup.Jsoup','lib/jsoup-1.6.3.jar') />
I recommend doing this with jQuery:
​$(document).ready(function(){
$($('form tr')[3]).after('<tr><td>row</tr></tr>');
});​
Much easier.

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>