css not working in cffile write - coldfusion

I have a report which is enclosed in a cfsavecontent tag.
<cfsavecontent variable = "testcust">
When I use
<cfoutput> #testcust# </cfoutput>
the report renders correctly. When I use cffile to save it to disk I lose the css formatting:
<cffile action = "write"
file = "/var/www/reports/testcust.cfm"
output = "#testcust#">
I can run testcust.cfm and it produces the report. All in-line styling is preserved. But formatting from the style sheet is not there. Does anyone know how to fix this?

You need to escape the html special characters. e.g < for < and > for >
So replace them before writing to file.

Related

coldfusion 10 cfinclude gives site cannot be reached error

I have a action page that redirects to a page based on condition.
I thought of Including that page insted of redirection.
So i used cfinclude function to include this file.
But including that file doesn't work.
But when i open that page in browser it is working fine without error.
For Ex.
http://domain.com/page2.cfm?cutomerID=10
is working fine.
IN http://domain.com/page1.cfm
I am including below code.
<cfset url.customerID = 10>
<cfinclude template="page2.cfm">
Even i tried to add only HTML content which i get in page source for "http://domain.com/page2.cfm?cutomerID=10"
It is still not working including a coldfusion page with just html text without any coldfusion code.
Strange thing is When i tried to remove some text from this html content it works for some time but when i reload it stops and give the below error again.
I am not sure if cfinclude has to do anything page length or some other factors.
Screenshot for error is attached below.
Error Screenshot Image
If you are asking for a link you can have it as:
<cfoutput>
<cfset href="domain.com/page2.cfm?customerID=#customerid#&etc=#etc#" />
</cfoutput>
For your question you can use this:
<cfset custID = 10/>
<cfinclude template="page2.cfm">
In page2.cfm:
<cfif isDefined(url.customerID) || isDefined(custID)>
<!--- your etc code here --->
</cfif>
Note: If you don't want to use url.customerID you can simply delete the occurance

cffile creating blank lines when storing a table

I've got a cfsavecontent tag that saves a table. Later I use cffile to write the saved content to a file. When I look at that file, I see that there many blank lines inserted after <td> tags in the table; and few blank lines inserted after </tr> tags. (Although it doesn't do that where the code says <tr><td> </td></tr> all on one line.)
Presently I have a file which contains two of those tables. The tables are generated in a loop, and the output file is created with cffile append. This file has 915 lines in it of which maybe 30 are non-blank. All my subsequent code works correctly, but this is just test data. In the real world I could have 1000 or more tables, and I am concerned about the file size.
The code:
<cfset head1 = 'from = "moxware" '>
<cfset head2 = 'to = "#hrep.PersonEmail#" '>
<cfset head3 = 'replyto = "#replyto#" '>
<cfset head4 = 'subject = "#subject#" '>
<cfset head5 = 'type = "html" '>
<cfsavecontent variable = "abc">
<cfoutput>
#head1#
#head2#
#head3#
#head4#
#head5# >
#xyz#
</cfoutput>
</cfsavecontent>
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfmail"
mode = "777" >
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "#abc#"
mode = "777">
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "</cfmail>"
mode = "777" >
Re the xyz, I am reading it in from a file:
<cffile action = "read"
file = "/var/www/reports/moxrep/#reportname#.cfm"
variable = "xyz">
and the file looks like this:
<link rel="stylesheet" href="sample.css">
<link rel="stylesheet" type = "text/css" href ="betty.css"/>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font- family:georgia,serif">Dear Customer,</span></span></p>
We were so pleased that you have signed up for one of our programs. Apparently you live in the city of {{1. Additionally we observe that your were referred to us by {{2. Below please find a listing of what you signed up for.</span></span></p>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font- family:georgia,serif">{{r</span></span></p>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">Sincerely Yours,</span></span></p>
<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">John Jones<br />
President<br />
XYZ Corporation</span></span></p>
The file was created by a code generator, not me, so it's a bit cumbersome. Later in the code I replace everything starting with {{ ; in particular {{r gets replaced with a table, and that is where the additional space is coming from.
The append itself is not inserting any extra lines.
Does anyone know what is causing these extra blank lines in the file; and how to get rid of them?
Betty, typically you need to do this carefully if you want to avoid whitespace. In particular the use of cfoutput with a query will generate lines. So this code:
<table>
<cfoutput query="myquery">
<tr><td>#col1#</td><td>#col2#</td></tr>
</cfoutput>
</table>
will produce extra lines... but if you do this:
<cfsetting enablecfoutputonly="yes">
<cfoutput><table></cfoutput>
<cfloop query="myquery"><cfoutput><tr><td>#col1#</td><td>#col2#</td></tr></cfoutput></cfloop>
<cfoutput></table></cfoutput>
You would carefully control exactly what is allowed to be appended to the buffer. enableoutputonly does exactly what it says... it does not allow anything to "go to the buffer" unless it is enclosed in cfoutputs.
Hope this helps. As cameron says you should paste code for questions like this. That's where the answer will typically reside.
(you might also need to experiment with the "addnewline" attribute of cffile - depending on whether your problem is a line at the END of your file).
To answer your question regarding adding cfsetting... in your case you are writing CF code to a file that is then executed later (which by the way is not a great idea usually :). So in your first Append statement:
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfmail"
mode = "777" >
Change the "output" to be:
<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfsetting enablecfoutputonly=""yes""/> <cfmail"
mode = "777" >
But Betty - you will still need to remove the line breaks from your cfsavecontent (if that's where your whitespace is coming from) because they actually ARE inside of a cfoutput. Also, your code that creates the table you are inserting might be at fault - and it is not listed here.
Finally, since this is cfmail take a look at this post regarding line breaks that may or may not have some bearing - but at least gives you one more piece of information :)
http://www.coldfusionmuse.com/index.cfm/2006/4/12/cfmail.linebreak
You may consider using cfprocessingdirective around your cfsavecontent. There is a setting in CF administrator that universally either compresses or retains unnecessary whitespace, "Enable Whitespace Management" - http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf3638e6-7ffc.html . Using the suppressWhiteSpace attribute of cfprocessingdirective, you can override this setting for a particular page or part of a page. So in your case:
<cfprocessingdirective suppressWhiteSpace="true">
<cfsavecontent variable="myvar">....
...
...
</cfsavecontent>
</cfprocessingdirective>
may help. Likewise, to ensure the retention of whitespace when building text emails, you'd use suppressWhiteSpace="false".
Cheers,

cf10 unable to add text to HTML Head

I am getting the following error on a page we are loading:
coldfusion.runtime.CfErrorWrapper
Unable to add text to HTML HEAD tag.
[empty string]
caused by
Template
Unable to add text to HTML HEAD tag.
ColdFusion was unable to add the text you specified to the output stream. This is probably because you have already used a CFFLUSH tag in your template or buffered output is turned off.
I've done a sweep of all the files that are included in our application and cannot find anything that uses CFFlush.
output is set to 'no' on all cfcs and components. I also tried adding cfsetting showdebugoutput = no in a file. That didn't help.
I turned request debugging on in cfadmin and that didn't help.
The HTML Head works fine in other parts of our app, it just seems to be on this one page.
The only thing really different about this page is that it is a particularly long page.
If it's a particularly long page, then CF may be flushing the buffer on its own. If you check in the CFAdmin, on the settings page, there is a setting for Maximum Output Buffer size. I believe the default is 1024 KB. If your page is over 1 meg of content, then CF may flush the buffer before your <cfhtmlhead /> tag runs. Try increasing the buffer size, or changing the placement of the <cfhtmlhead /> tag to see if that corrects the issue.
I've run into the same problem recently but the behavior wasn't predictable. I believe that Dan Short's answer is correct. I created some test pages to see if I could reproduce the problem. Each time TestTemplate.cfm is included, CFHTMLHEAD writes a simple JavaScript alert to the head tag. Once the buffer is reached, and the page is automatically flushed, any subsequent CFHTMLHEAD tag use will result in an error, specifically, the error in the original post. As Dan indicates, you can work your way around this issue by changing the maximum output buffer size.
file: index.cfm
<html>
<head><title>Test Page</title></head>
<body>
<cfset SampleScript = "<script src='sample.js'></script>">
cfset Count = 0>
<cfinclude template="TestTemplate.cfm">
<cfinclude template="TestTemplate.cfm">
<cfinclude template="TestTemplate.cfm">
</body>
</html>
file TestTemplate.cfm
<cfhtmlhead text="#SampleScript#">
<cfset Count++>
<cfoutput>
<h1>Count #Count#</h1>
</cfoutput>
<cfoutput>
<cfloop from="1" to="100000" index="i">
<cfscript>
j = randRange(i, 1000000);
k = randRange(i, 1000000);
l = j * k;
writeOutput(l);
</cfscript>
</cfloop>
</cfoutput>
file sample.js
alert('Boo!');
server.log showed another error that I was submitting too many fields with a POST request. I had to increase this limit on the Settings page.
To fix this, login to Coldfusion Admin, go to Memory Variables, and uncheck 'Disable updating Coldfusion internal cookies using Coldfusion tags/functions.' Save your settings and restart your website.

White Space / Coldfusion

What would be the correct way to stop the white space that ColdFusion outputs?
I know there is cfcontent and cfsetting enableCFoutputOnly. What is the correct way to do that?
In addition to <cfsilent>, <cfsetting enablecfoutputonly="yes"> and <cfprocessingdirective suppressWhiteSpace = "true"> is <cfcontent reset="true" />. You can delete whitespaces at the beginning of your document with it.
HTML5 document would then start like this:
<cfcontent type="text/html; charset=utf-8" reset="true" /><!doctype html>
XML document:
<cfcontent reset="yes" type="text/xml; charset=utf-8" /><CFOUTPUT>#VariableHoldingXmlDocAsString#</CFOUTPUT>
This way you won't get the "Content is not allowed in prolog"-error for XML docs.
If you are getting unwanted whitespaces from a function use the output-attribute to suppress any output and return your result as string - for example:
<cffunction name="getMyName" access="public" returntype="string" output="no">
<cfreturn "Seybsen" />
</cffunction>
You can modify the ColdFusion output by getting access to the ColdFusion Outpout Buffer. James Brown recently demo'd this at our user group meeting (Central Florida Web Developers User Group).
<cfscript>
out = getPageContext().getOut().getString();
newOutput = REreplace(out, 'regex', '', 'all');
</cfscript>
A great place to do this would be in Application.cfc onRequestEnd(). Your result could be a single line of HTML which is then sent to the browser. Work with your web server to GZip and you'll cut bandwidth a great deal.
In terms of tags, there is cfsilent
In the administrator there is a setting to 'Enable whitespace management'
Futher reading on cfsilent and cfcontent reset.
If neither <cfsilent> nor <cfsetting enablecfoutputonly="yes"> can satisfy you, then you are probably over-engineering this issue.
When you are asking solely out of aesthetic reasons, my recommendation is: Ignore the whitespace, it does not do any harm.
Alternatively, You can ensure your entire page is stored within a variable and all this processing is done within cfsilent tags. e.g.
<cfsilent>
<!-- some coldfusion -->
<cfsavecontent variable="pageContent">
<html>
<!-- some content -->
</html>
</cfsavecontent>
<!-- reformat pageContent if required -->
</cfsilent><cfoutput>#pageContent#</cfoutput>
Finally, you can perform any additional processing after you've generated the pagecontent but before you output it e.g. a regular expression to remove additional whitespace or some code tidying.
Here's a tip if you use CFC.
If you're not expecting your method to generate any output, use output="false" in <cffunction> and <cfcomponent> (not needed only if you're using CF9 script style). This will eliminate a lot of unwanted whitespaces.
If you have access to the server and want to implement it on every page request search for and install trimflt.jar. It's a Java servlet filter that will remove all whitespace and line breaks before sending it off. Drop the jar in the /WEB-INF/lib dir of CF and edit the web.xml file to add the filter. Its configurable as well to remove comments, exclude files or extensions, and preserve specific strings. Been running it for a few years without a problem. A set it and forget it solution.
I've found that even using every possible way to eliminate whitespace, your code may still have some unwanted spaces or line breaks. If you're still experiencing this you may need to sacrifice well formatted code for desired output.
for example, instead of:
<cfprocessingdirective suppressWhiteSpace = "true">
<cfquery ...>
...
...
...
</cfquery>
<cfoutput>
Welcome to the site #query.userName#
</cfoutput>
</cfprocessingdirective>
You may need to code:
<cfprocessingdirective suppressWhiteSpace = "true"><cfquery ...>
...
...
...
</cfquery><cfoutput>Welcome to the site #query.UserName#</cfoutput></cfprocessingdirective>
This isn't CF adding whitespace, but you adding whitespace when formatting your CF.
HTH

How to generate PDF from .cfm page in Mac

I am using <cfdocument> tag of coldfusion 7. Using CFEclipse and working on MacOS.
I have written the following code:
<cfdocument format="pdf">
SitePoint.com - Introduction to ColdFusion 7
PDF Generation
This is an example of PDF generation using ColdFusion 7.
</cfdocument>
But instead of asking me to save this file in .pdf format, its trying to open it in .cfm format.
How can I save it in .pdf format? Thanks!
Unless you tell it otherwise, the webserver returns the results of a CFM call as text. You need to use CFContent with CFHeader to alert the browser that the results it will be recieving are of a different type. Something like:
<cfheader name="Content-Disposition" value="inline; filename=document.pdf">
<cfcontent type="application/x-pdf">
<cfdocument>...</cfdocument>
I may have the MIME type wrong there. I'm doing this from memory. Check the docs for more help.
If you are on CF8 or higher then use the saveAsName attribute:
<cfdocument saveAsName=""
Either that or the method suggested by Ben above should work
You might also need to import the style sheet as well. So you can get the desired formatting. It needs to be imported after cfdocument.
<cfdocument
format="pdf"
filename = "canwriteurfile.pdf"
overwrite = "yes"
marginBottom = ".2"
marginLeft = ".4"
marginRight = ".4"
marginTop = ".2">
<style type="text/css">#import "pdf.css";</style>
BLAHHH BLAHHH PDF FORMATTING STUFF
</cfdocument>
Try:
<cfdocument format="PDF" name="myPDF">
<html>
<body>
SitePoint.com - Introduction to ColdFusion 7
PDF Generation
This is an example of PDF generation using ColdFusion 7.
</body>
</html>
</cfdocument>
<cfpdf action = "write" destination = "pdf_path" source = "myPDF" overwrite = "yes"/>
<cflocation url="pdf_path"/>
Whis this you save the PDF on disk