I'm trying to understand ColdFusion, as I am coming from ASP.NET. I've put together a sample page to piggy-back off some already finished code -- but I can't seem to get the actual data from the object I am creating:
<cfset objProduct = createObject("component", "com.MyObj.Product")>
<cfset prodExists = objProduct.getProduct(10)>
<html>
<head/>
<body>
<h2>#objProduct.ProductName#</h2>
</body>
... It simply prints the literal #objProduct.ProductName# text, and not the data from within the object. Any idea where I'm going astray?
Thanks!
Don't forget the <cfoutput>!
<cfoutput>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2>#HtmlEditFormat(objProduct.ProductName)#</h2>
</body>
</html>
</cfoutput>
You forgot the CFOUTPUT tags.
You're trying to output the wrong variable.
#objProduct.getProduct(10)#
or
<cfoutput>#prodExists#</cfoutput>
Related
I am at learning stage in ColdFusion. Now I am trying to execute below code to display variable content by using <cfset> and <cfoutput> tags. I completed my server installation and I am able to login to server with admin credentials. But it executed as normal html page, not getting the result from ColdFusion tags.
Do I need to install anything apart from this?
<!DOCTYPE html>
<html>
body>
<body background="download.jpg">
<cfset name ="swanav"/>
<cfoutput>#name#</cfoutput>
</body>
</html>
A small correction to your code,
<!DOCTYPE html>
<html>
<body background="download.jpg">
<cfset name="swanav" />
<cfoutput>#name#</cfoutput>
</body>
</html>
The code needs to be placed in,
/cfusion/wwwroot
The project I have is to create a Vista Print like application. I have created a very basic interface that places a resizable div in an area that can be dragged around. I am also able to place text into this div via pop-up iframe with designmode. I then write this to a database.
I then call this data and create a pdf using Coldfusions cfdocument.
The issue is when the PDF is created the font looks slightly thicker and the word wrap is different than the html page interface which is very important. Note: The word wraps are determined by the divs height and width. It looks fine on the cfdocument process page it is just off when the pdf is created. Anyone have any ideas on this or can point me to some info that could help?
Thanks in advance.
Code Here:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<cfset webRes = "96">
<cfset pxlWidth = "921">
<cfset pxlHeight = "1178">
<cfset inWidth = (pxlWidth/webres)>
<cfset inHeight = (pxlHeight/webres)>
<cfquery name="qTextData" datasource="ds">
SELECT DocID, TextID, Width, Height, PosX, PosY, FontFamily, FontColor, FontSize, TextValue
FROM TextEditor
WHERE ((DocID = #session.docid#))
</cfquery>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create PDF</title>
</head>
<cfoutput>
<cfdocument filename="mypdf.pdf" name="mypdf" fontembed="yes" format="pdf" overwrite="yes" pageheight="#inHeight#" pagewidth="#inWidth#" pagetype="custom">
<cfdocumentsection margintop="0" marginright="0" marginbottom="0" marginleft="0">
<body align="center" marginheight="0" marginwidth="0">
<div id="pageContainer" style="background-image:url(resources/image.caspx.jpg); background-repeat:no-repeat; height:#pxlHeight#px; width:#pxlWidth#px;">
<cfloop query="qTextData">
<div id="divTextField_#TextID#" style="position:absolute; top:#PosY#px; left:#PosX#px; width:#width#px; height:#Height#px; color:#FontColor#; font-family:#FontFamily#; font-size:#FontSize#pt; vertical-align:text-top; line-spacing:normal;">
#TextValue#
</div>
</cfloop>
</div>
</body>
</cfdocumentsection>
</cfdocument>
</cfoutput>
<script type="text/javascript">
window.open('http://linktopdf/mypdf.pdf','newWin','resizable=1');
</script>
</html>
I tried making this a comment but I started getting long winded and it was too large so it may not be the "answer" but if nothing else I'm sure it will have some helpful information in it.
What % are you viewing at and have you tried printing it out?
I've noticed that things can look lopsided or bigger than usual because they are just rendered weird in acrobat (or your PDF viewer of choice). Try adjusting the zoom and see if the display changes, also see if it prints correctly.
I have also found that like James suggested, I need special css just for the document. The CSS could be as odd as making one side of the div have a 1px larger border to make it look correct. Also keep in mind that although you view PDFs on the screen it is still a "print" media. PT means 1/72 of an inch which will look bigger when you scale from your 19+ inch monitor (which is further effected by your pixel resolution) to an 8.5" x 11" piece of paper that doesn't truly know what a pixel is. You may need to try adjusting your font size on the fly, something like: font-size: #fontSize-2#pt;. Change -2 to anything that may work for your output. You could also try using a unit that scales, like em.
The cfdocument docs list all the CSS that is supported as well as identify some restrictions. It does not appear that line-spacing:normal; is supported. Also make sure all your CSS is included inline or in a style tag but avoid using a linked .css file. I see you're doing that, just adding tips and pointers.
Also, according to the docs:
ColdFusion does not return HTML and CFML outside of
the <cfdocument></cfdocument> pair.
That means ColdFusion is processing your cfset statements but it does not return
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create PDF</title>
</head>
and
<script type="text/javascript">
window.open('http://linktopdf/mypdf.pdf','newWin','resizable=1');
</script>
</html>
So it is possible that iText isn't rendering it as you would expect because
It does not have a doctype
It is an ill-formed HTML document
I am attempting to use XMLParse against content that is not valid xhtml. In the html code, the tags are not properly terminated. In order to terminate them, I am using the replace function to find the invalid code and replace it with properly terminated code. In doing so, my application errors out and tells me that the meta tag is invalid:
An error occured while Parsing an XML document.
The element type "meta" must be terminated by the matching end-tag "".
The code I'm trying to validate is:
<html>
<head>
<title>Impart Client Interface</title>
<link href="side_panel.css" rel="stylesheet" type="text/css">
<link href="default.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
<link href="data_tables.css" rel="stylesheet" type="text/css">
<link href="xp_button.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
What I've created in CF to attempt to handle this is:
<cfset xml = objResponse.FileContent>
<cfset page.content = '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'>
<cfset page.updatedcontent = replace('#page.content#','8859-1','8859-1" />"','')>
<Cfset page.link = 'type="text/css">'>
<cfset page.updatedLink = replace('#page.link#', 'css">', 'css" />', 'all')>
<cfset validXML = replace(#xml#, "#page.content#", "#page.updatedContent#", "")>
<cfset validXML = replace(#xml#, "#page.link#", "#page.UpdatedLink#", "all")>
<cfoutput>#validXML#</cfoutput>
<cfset parsethis = xmlparse(validXML)>
<cfdump var="#parsethis#">
How can I resolve this error?
Looks to me like you are missing part of the substring in your replace call:
<cfset page.updatedcontent = replace(page.content,'8859-1">','8859-1" />')>
Note the addition of ">
So, the above will address your specific technical question. I'd like to suggest a better approach to your general task, however. Doing string manipulation on HTML to try to mash it into proper XHTML is tricky at best (as you have seen). Instead, consider abandoning XMLParse in favor of an actual HTML parser, such as JSOUP. After you download the jar and add it to your CF classpath, you can do things like this:
<cfset jsoup = CreateObject("java", "org.jsoup.Jsoup")>
<cfsavecontent variable="html">
<html>
<body>
<hr>
<pre id="blah">Foo<br>bar1</pre>
<hr>
<pre id="blah2">Foo<br>bar2</pre>
</body>
</html>
</cfsavecontent>
<cfdump var="#jsoup.parse(html).select('pre').first().html()#">
Which will output :
Foo<br />bar1
Pretty spiffy, eh? And no need to pull out your hair over getting exact details right with XML.
I was trying to run this code, and browser ask me to download pdf file which is good, But it was not loading remaining HTML code or you can say I could not see "Thank you Message".
Am I doing something wrong or missing something?
Need your help
<cfheader name="content-disposition" value="attachment; filename=134.pdf"/>
<cfcontent type="application/pdf" file="#ExpandPath( './134.pdf' )#"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Download Now</title>
</head>
Thank you for your interest in downloading
this file. Your download should begin shortly.
Maybe show the 'download will begin shortly' message first as a distinct page, which then redirects (JavaScript or meta tag...) to the .cfm that generates the PDF.
What you're trying to do is load the PDF file immediatly. As stated before you should have a basic HTML Page:
<html>
<head>
<meta http-equiv="refresh" content="2;url=getpdf.cfm?file=123.pdf">
..rest of head
</head>
<body>
Download will begin shortly.
</body>
</html>
This getpdf.cfm?file=123.pdf will be
<cfheader name="content-disposition" value="attachment; filename=134.pdf"/>
<cfcontent type="application/pdf" file="#ExpandPath( './134.pdf' )#"/>
The reason it wont work, is that you browser reads the "headers" and will basically see the file as a PDF file. Like you were downloading a ".pdf". Now you're putting one step in between and let the browser redirect to the PDF download.
As a simple example of the problem:
<cfsavecontent variable = "pageOutput">
<cfoutput>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>#page_title#</title>
</head>
<body>
<cfdump var="#URL#">
</body>
</html>
</cfoutput>
</cfsavecontent>
I save this page to disk. Then when I call this newly-generated page via the following URL:
http://blah/products.cfm?search_keyword=bathroom&search_category=451&search_province=Auckland
The dump doesn't display the newly-passed url vars.
I'm obviously missing something pretty basic here.
What you need to do is replace the opening or closing portion of the tag so that when it gets output you have a valid tag. Something like this:
<cfsavecontent variable="page">
<cfset sCfO = "<" >
<cfset sCfC = "</" >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><cfoutput>#page_title#</cfoutput></title>
</head>
<body>
<cfoutput>#sCFO#</cfoutput>cfdump var="#URL#">
<cfoutput>#sCFO#</cfoutput>cfoutput>
#URL.myMessage#
<cfoutput>#sCFC#</cfoutput>cfoutput>
</body>
</html>
Hopefully that makes sense...
If I understand your explanations correctly, in your generated with cfsavecontent pagevariables are already substituted, so you have the static HTML in products.cfm. Try to open it in editor and review the code.
I have a question: what is the purpose of using cfsavecontent here? What are you trying to achieve? Looks like you may not need it at all, plain ol' CFML should do the job.
I would be more inclined to hide that in a function which would read much nicer in your example you could call dumpVariable(url) and encapsulate that in a function. We actually had to do something similar by parsing custom tags from a database into a render function that used cfmodule for the actual implementation. I was pretty happy with the results and it was much easier to tell what was going on in the code than arbitrary character replacement.