• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 86
  • Last Modified:

How can I create output file in coldfusion

The following coldfusion code will create a image, out put to screen, I need them output to a file,  Thank you very much:

<cfset LAST_NAME = 'Smith'>
<cfset First_NAME = 'John'>
<cfset m_initial = 'D'>

<cfset address_line1 = '123 Main St'>
<cfset address_line2 = 'Unit 1'>
<cfset city = 'Chicago'>
<cfset state = 'IL'>
<cfset zip_code = '60016'>
<cfset AreaCode = '312'>
<cfset home_phone = '225-2123'>

<style type="text/css">
<!--
.style1 {
      font-family: "Wingdings 2";
      font-size: 21px;
      font-weight:bold;
      text-align:center;
}
.style2 {
      font-family: Arial, Helvetica, sans-serif;
      font-weight: bold;
      
}


#Layer1 {
      position:absolute;
      width:1020px;
      height:1320px;
      z-index:4;
      left: 1px;
      top: 13px;
}
#Text_Align_Bottom {
      position: absolute;
      z-index: 1;
      bottom: 1px;
      left: 0px;
      width: 100%;
}
#INSURANCE {
      position: absolute;
      width: 16.67px;
      height: 16.67px;
      z-index: 2;
      left: 398px;
      top: 168px;
}
#P_NAME {
      position:absolute;
      width:332px;
      height:25px;
      z-index:3;
      left: 48px;
      top: 205px;
}
#P_ADDRESS {
      position:absolute;
      width:332px;
      height:25px;
      z-index:1;
      left: 48px;
      top: 245px;
}
#P_CITY {
      position:absolute;
      width:285px;
      height:25px;
      z-index:4;
      left: 48px;
      top: 284px;
      
}
#P_STATE {
      position:absolute;
      width:34px;
      height:25px;
      z-index:5;
      left: 345px;
      top: 284px;
}
#P_ZIP {
      position:absolute;
      width:145px;
      height:25px;
      z-index:6;
      left: 48px;
      top: 325px;
}
#P_AREA_CODE {
      position:absolute;
      width:30px;
      height:25px;
      z-index:7;
      left: 214px;
      top: 325px;
}
#P_PHONE_NO {
      position:absolute;
      width:122px;
      height:25px;
      z-index:8;
      left: 260px;
      top: 325px;
}
#P_EMPLOYMENT_NO {
      position: absolute;
      width: 16.67px;
      height: 16.67px;
      z-index: 10;
      left: 519px;
      top: 409px;
}
#P_AUTO_ACCIDENT_NO {
      position: absolute;
      width: 16.67px;
      height: 16.67px;
      z-index: 10;
      left: 519px;
      top: 447px;
}
#P_OTHER_ACCIDENT_NO {
      position: absolute;
      width: 16.67px;
      height: 16.67px;
      z-index: 10;
      left: 519px;
      top: 487px;
}

#P_SIGNATURE {
      position:absolute;
      width:223px;
      height:25px;
      z-index:1;
      left: 108px;
      top: 602px;
}

#P_NPI {
      position:absolute;
      width:103px;
      height:25px;
      z-index:1;
      left: 652px;
      top: 1227px;
}
-->
</style>

<cfoutput>
      <div id="Layer1">
            <div class="style1" id="INSURANCE">&ETH;</div>
              <div class="style2" id="P_NAME"><div id="Text_Align_Bottom">#first_name#, #last_name# #m_initial# </div></div>
            <div class="style2" id="P_ADDRESS"><div id="Text_Align_Bottom">#address_line1# #address_line2#</div></div>
            <div class="style2" id="P_CITY"><div id="Text_Align_Bottom">#city#</div></div>
            <div class="style2" id="P_STATE"><div id="Text_Align_Bottom">#state#</div></div>
            <div class="style2" id="P_ZIP"><div id="Text_Align_Bottom">#zip_code#</div></div>
            <div class="style2" id="P_AREA_CODE"><div id="Text_Align_Bottom">#(AreaCode)#</div></div>
            <div class="style2" id="P_PHONE_NO"><div id="Text_Align_Bottom">#home_phone#</div></div>
            <div class="style1" id="P_EMPLOYMENT_NO">&ETH;</div>
            <div class="style1" id="P_AUTO_ACCIDENT_NO">&ETH;</div>
            <div class="style1" id="P_OTHER_ACCIDENT_NO">&ETH;</div>
          <div class="style2" id="P_SIGNATURE"><div id="Text_Align_Bottom">SIGNATURE ON FILE</div></div>
            <div class="style2" id="P_NPI"><div id="Text_Align_Bottom">1234567890</div></div>
      </div>
</cfoutput>
0
jfreeman2010
Asked:
jfreeman2010
  • 17
  • 12
1 Solution
 
jfreeman2010Author Commented:
I need to output to a .txt file.
0
 
LajuanTaylorCommented:
Are looking to something like the following? I added a couple statements to your original sample:

 <!--- Get expanded base path of the current template --->
<cfset strPath = ExpandPath( "./" ) />
 
 <cfset LAST_NAME = 'Smith'>
 <cfset First_NAME = 'John'>
 <cfset m_initial = 'D'>

 <cfset address_line1 = '123 Main St'>
 <cfset address_line2 = 'Unit 1'>
 <cfset city = 'Chicago'>
 <cfset state = 'IL'>
 <cfset zip_code = '60016'>
 <cfset AreaCode = '312'>
 <cfset home_phone = '225-2123'>

 <style type="text/css">
 <!--
.style1 {
       font-family: "Wingdings 2";
       font-size: 21px;
       font-weight:bold;
       text-align:center;
 }
 .style2 {
       font-family: Arial, Helvetica, sans-serif;
       font-weight: bold;
       
 }


 #Layer1 {
       position:absolute;
       width:1020px;
       height:1320px;
       z-index:4;
       left: 1px;
       top: 13px;
 }
 #Text_Align_Bottom {
       position: absolute;
       z-index: 1;
       bottom: 1px;
       left: 0px;
       width: 100%;
 }
 #INSURANCE {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 2;
       left: 398px;
       top: 168px;
 }
 #P_NAME {
       position:absolute;
       width:332px;
       height:25px;
       z-index:3;
       left: 48px;
       top: 205px;
 }
 #P_ADDRESS {
       position:absolute;
       width:332px;
       height:25px;
       z-index:1;
       left: 48px;
       top: 245px;
 }
 #P_CITY {
       position:absolute;
       width:285px;
       height:25px;
       z-index:4;
       left: 48px;
       top: 284px;
       
 }
 #P_STATE {
       position:absolute;
       width:34px;
       height:25px;
       z-index:5;
       left: 345px;
       top: 284px;
 }
 #P_ZIP {
       position:absolute;
       width:145px;
       height:25px;
       z-index:6;
       left: 48px;
       top: 325px;
 }
 #P_AREA_CODE {
       position:absolute;
       width:30px;
       height:25px;
       z-index:7;
       left: 214px;
       top: 325px;
 }
 #P_PHONE_NO {
       position:absolute;
       width:122px;
       height:25px;
       z-index:8;
       left: 260px;
       top: 325px;
 }
 #P_EMPLOYMENT_NO {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 10;
       left: 519px;
       top: 409px;
 }
 #P_AUTO_ACCIDENT_NO {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 10;
       left: 519px;
       top: 447px;
 }
 #P_OTHER_ACCIDENT_NO {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 10;
       left: 519px;
       top: 487px;
 }

 #P_SIGNATURE {
       position:absolute;
       width:223px;
       height:25px;
       z-index:1;
       left: 108px;
       top: 602px;
 }

 #P_NPI {
       position:absolute;
       width:103px;
       height:25px;
       z-index:1;
       left: 652px;
       top: 1227px;
 }
 -->
 </style>
<!---  Use cfcontent to store div content in variable for file output --->
<cfsavecontent variable="myFileOutput">
 <cfoutput>
       <div id="Layer1">
             <div class="style1" id="INSURANCE">&ETH;</div>
              <div class="style2" id="P_NAME"><div id="Text_Align_Bottom">#first_name#, #last_name# #m_initial# </div></div>
             <div class="style2" id="P_ADDRESS"><div id="Text_Align_Bottom">#address_line1# #address_line2#</div></div>
             <div class="style2" id="P_CITY"><div id="Text_Align_Bottom">#city#</div></div>
             <div class="style2" id="P_STATE"><div id="Text_Align_Bottom">#state#</div></div>
             <div class="style2" id="P_ZIP"><div id="Text_Align_Bottom">#zip_code#</div></div>
             <div class="style2" id="P_AREA_CODE"><div id="Text_Align_Bottom">#(AreaCode)#</div></div>
             <div class="style2" id="P_PHONE_NO"><div id="Text_Align_Bottom">#home_phone#</div></div>
             <div class="style1" id="P_EMPLOYMENT_NO">&ETH;</div>
             <div class="style1" id="P_AUTO_ACCIDENT_NO">&ETH;</div>
             <div class="style1" id="P_OTHER_ACCIDENT_NO">&ETH;</div>
           <div class="style2" id="P_SIGNATURE"><div id="Text_Align_Bottom">SIGNATURE ON FILE</div></div>
             <div class="style2" id="P_NPI"><div id="Text_Align_Bottom">1234567890</div></div>
       </div>
 </cfoutput> 
</cfsavecontent>

<!--- .htm allows content to display in browser, .txt literally places your div content in text file --->
<cffile action="write" file="#strPath#sample.htm" output="#myFileOutput#">
</html>

Open in new window

0
 
jfreeman2010Author Commented:
LajuanTayor - thank you very much for the reply and help.  I think this is what I am looking for.  I just have hard time to write out this file.  I try to just write out to c:\temp.  I am still try...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
jfreeman2010Author Commented:
I replace this: <cfset strPath = ExpandPath( "./" ) />  
with this:<cfset FileName = "C:\temp\TestImageFile">

and

<cffile action="write" file="#strPath#sample.htm" output="#myFileOutput#">
with
<cffile action="write" file="#FileName#.txt" output="#myFileOutput#">


unable to find the file....
0
 
jfreeman2010Author Commented:
Hi LajuanTayor,

I was able to write out the file.  The file open look like this:
<div id="Layer1">
                        <div class="style1" id="INSURANCE">&ETH;</div>
                        <div class="style2" id="P_NAME"><div id="Text_Align_Bottom">John, Smith D </div></div>
                        <div class="style2" id="P_ADDRESS"><div id="Text_Align_Bottom">123 Main St Unit 1</div></div>
                        <div class="style2" id="P_CITY"><div id="Text_Align_Bottom">Chicago</div></div>
                        <div class="style2" id="P_STATE"><div id="Text_Align_Bottom">IL</div></div>
                        <div class="style2" id="P_ZIP"><div id="Text_Align_Bottom">60016</div></div>
                        <div class="style2" id="P_AREA_CODE"><div id="Text_Align_Bottom">312</div></div>
                        <div class="style2" id="P_PHONE_NO"><div id="Text_Align_Bottom">225-2123</div></div>
                        <div class="style1" id="P_EMPLOYMENT_NO">&ETH;</div>
                        <div class="style1" id="P_AUTO_ACCIDENT_NO">&ETH;</div>
                        <div class="style1" id="P_OTHER_ACCIDENT_NO">&ETH;</div>
                        <div class="style2" id="P_SIGNATURE"><div id="Text_Align_Bottom">SIGNATURE ON FILE</div></div>
                        <div class="style2" id="P_NPI"><div id="Text_Align_Bottom">1234567890</div></div>
                  </div>
0
 
LajuanTaylorCommented:
If you want just the data, you can leave out the div formatting code.

As for the finding the file, you have to setup a virtual directory since the path is outside of the wwwroot. I'm assuming you are running IIS as the web server.
0
 
jfreeman2010Author Commented:
I was able to out put the file.  I do need the div to formatting the code (position the output value) for the out file.  I will try to move out the cfoutput out side of cfsavecontent to see if it output the format.
0
 
LajuanTaylorCommented:
Are the text files being sent to another content management tool?
0
 
jfreeman2010Author Commented:
yes, it need on the format what required by another management tool.  I only need to create a .txt file, format as div layout.
0
 
LajuanTaylorCommented:
I think this might work for you. I found that you can remove the extra space by using the trim function:
<cffile action="write" file="#strPath#sample.txt" output="#trim(myFileOutput)#">

Open in new window

0
 
jfreeman2010Author Commented:
The file (output) looks like this:
<div id="Layer1">
                        <div class="style1" id="INSURANCE">&ETH;</div>
                        <div class="style2" id="P_NAME"><div id="Text_Align_Bottom">John, Smith D </div></div>
                        <div class="style2" id="P_ADDRESS"><div id="Text_Align_Bottom">123 Main St Unit 1</div></div>
                        <div class="style2" id="P_CITY"><div id="Text_Align_Bottom">Chicago</div></div>
                        <div class="style2" id="P_STATE"><div id="Text_Align_Bottom">IL</div></div>
                        <div class="style2" id="P_ZIP"><div id="Text_Align_Bottom">60016</div></div>
                        <div class="style2" id="P_AREA_CODE"><div id="Text_Align_Bottom">312</div></div>
                        <div class="style2" id="P_PHONE_NO"><div id="Text_Align_Bottom">225-2123</div></div>
                        <div class="style1" id="P_EMPLOYMENT_NO">&ETH;</div>
                        <div class="style1" id="P_AUTO_ACCIDENT_NO">&ETH;</div>
                        <div class="style1" id="P_OTHER_ACCIDENT_NO">&ETH;</div>
                        <div class="style2" id="P_SIGNATURE"><div id="Text_Align_Bottom">SIGNATURE ON FILE</div></div>
                        <div class="style2" id="P_NPI"><div id="Text_Align_Bottom">1234567890</div></div>
                  </div>
0
 
jfreeman2010Author Commented:
It did not output the value in the right position.
0
 
jfreeman2010Author Commented:
This:  <div class="style1" id="INSURANCE">&ETH;</div>, should output to file as "X", in position as left: 398px; top: 168px.

see style, #INSURANCE.

I am not sure if coldfusion able to write out file with div value....
0
 
LajuanTaylorCommented:
Here's the gotcha... If you view the saved .txt as .htm with the css applied, the X does display in the browser and positioned where you want it.

Is your other content tool going to include the css or do you wish to include the css from your sample?

Also, the div value without the style applied becomes an old English letter if you view the text file as html:
http://www.bing.com/search?q=%26ETH%3B&src=IE-SearchBox&FORM=IENTTR&conversationid=

If "&ETH;" is going to always represent "X", maybe you can perform a server side replace with X before writing out to the text file. This might eliminate the need to include the "Wingdings 2" font family.
0
 
jfreeman2010Author Commented:
I had to use css to position the value as the sample code above.  The output file will show "X" when in the code as "&ETH".  

I am not sure other way in coldfusion that can write a .txt file will position values.

Do you think is posible what I am output the about to a txt file?

Thank you very much for your help....
0
 
jfreeman2010Author Commented:
I think I can use 'X' instead of '&ETH' here, but still need the position to print this 'X'
0
 
LajuanTaylorCommented:
Just include your style inside the cfsavecontent. Note use of the double pounds on the css. When view inside the browser from your other content tool the position should be in place:
0
 
LajuanTaylorCommented:
<cfoutput>
<cfsavecontent variable="myFileOutput">
<style type="text/css">
 <!--
.style1 {
       font-family: "Wingdings 2";
       font-size: 21px;
       font-weight:bold;
       text-align:center;
 }
 .style2 {
       font-family: Arial, Helvetica, sans-serif;
       font-weight: bold;
       
 }


 ##Layer1 {
       position:absolute;
       width:1020px;
       height:1320px;
       z-index:4;
       left: 1px;
       top: 13px;
 }
##Text_Align_Bottom {
       position: absolute;
       z-index: 1;
       bottom: 1px;
       left: 0px;
       width: 100%;
 }
 ##INSURANCE {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 2;
       left: 398px;
       top: 168px;
 }
 ##P_NAME {
       position:absolute;
       width:332px;
       height:25px;
       z-index:3;
       left: 48px;
       top: 205px;
 }
 ##P_ADDRESS {
       position:absolute;
       width:332px;
       height:25px;
       z-index:1;
       left: 48px;
       top: 245px;
 }
 ##P_CITY {
       position:absolute;
       width:285px;
       height:25px;
       z-index:4;
       left: 48px;
       top: 284px;
       
 }
 ##P_STATE {
       position:absolute;
       width:34px;
       height:25px;
       z-index:5;
       left: 345px;
       top: 284px;
 }
 ##P_ZIP {
       position:absolute;
       width:145px;
       height:25px;
       z-index:6;
       left: 48px;
       top: 325px;
 }
 ##P_AREA_CODE {
       position:absolute;
       width:30px;
       height:25px;
       z-index:7;
       left: 214px;
       top: 325px;
 }
 ##P_PHONE_NO {
       position:absolute;
       width:122px;
       height:25px;
       z-index:8;
       left: 260px;
       top: 325px;
 }
 ##P_EMPLOYMENT_NO {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 10;
       left: 519px;
       top: 409px;
 }
 ##P_AUTO_ACCIDENT_NO {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 10;
       left: 519px;
       top: 447px;
 }
 ##P_OTHER_ACCIDENT_NO {
       position: absolute;
       width: 16.67px;
       height: 16.67px;
       z-index: 10;
       left: 519px;
       top: 487px;
 }

 ##P_SIGNATURE {
       position:absolute;
       width:223px;
       height:25px;
       z-index:1;
       left: 108px;
       top: 602px;
 }

 ##P_NPI {
       position:absolute;
       width:103px;
       height:25px;
       z-index:1;
       left: 652px;
       top: 1227px;
 }
 -->
 </style>
       <div id="Layer1">
             <div class="style1" id="INSURANCE">X</div>
              <div class="style2" id="P_NAME"><div id="Text_Align_Bottom">#first_name#, #last_name# #m_initial# </div></div>
             <div class="style2" id="P_ADDRESS"><div id="Text_Align_Bottom">#address_line1# #address_line2#</div></div>
             <div class="style2" id="P_CITY"><div id="Text_Align_Bottom">#city#</div></div>
             <div class="style2" id="P_STATE"><div id="Text_Align_Bottom">#state#</div></div>
             <div class="style2" id="P_ZIP"><div id="Text_Align_Bottom">#zip_code#</div></div>
             <div class="style2" id="P_AREA_CODE"><div id="Text_Align_Bottom">#(AreaCode)#</div></div>
             <div class="style2" id="P_PHONE_NO"><div id="Text_Align_Bottom">#home_phone#</div></div>
             <div class="style1" id="P_EMPLOYMENT_NO">X</div>
             <div class="style1" id="P_AUTO_ACCIDENT_NO">X</div>
             <div class="style1" id="P_OTHER_ACCIDENT_NO">X</div>
           <div class="style2" id="P_SIGNATURE"><div id="Text_Align_Bottom">SIGNATURE ON FILE</div></div>
             <div class="style2" id="P_NPI"><div id="Text_Align_Bottom">1234567890</div></div>
       </div> 
</cfsavecontent>
</cfoutput>

Open in new window

0
 
jfreeman2010Author Commented:
the code now is show in browser correct, but not a txt file.  Is possible to write to a .txt file?
0
 
LajuanTaylorCommented:
Yes. Just change the file extension to .txt in the cffile. CF will write the file to a text file.

I'm assuming your content tool will parse the text file and apply the inline styling...
0
 
jfreeman2010Author Commented:
I changed the file extension to .txt and the .txt file include all the cfm code, include css content.

can I write out a text file? not cfm/html code file?

"I'm assuming your content tool will parse the text file and apply the inline styling." --- no, I only can send a txt file, not cfm/html code file
0
 
LajuanTaylorCommented:
Please paste a sample of exactly how you need the new text to look.

Also what is the source of the existing content - database query, static pages, or another URL?

I'm trying to determine if cfsavecontent versus another approach would be better for you.

Thanks.
0
 
jfreeman2010Author Commented:
see attached test file.
claims.Txt
0
 
jfreeman2010Author Commented:
data come from a db query.
0
 
LajuanTaylorCommented:
Okay, I see.

For this you are going to have painstakingly format and space the text the way want it to appear using the CF chr() function within the cfsavecontent area:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6e3b.html

I tried variations with cfhttp, cfheader, and cfcontent...

There is just no easy way to extract the data with positioning details and save it as text only. You have to format the text dynamically and save it to file.

If your content tool can parse XML, that would be a better option.
0
 
LajuanTaylorCommented:
Note how the following snippet saves to file as text only with the extra spaces:
<cfset strPath = ExpandPath( "./" ) />

<cfoutput>
        File: test.txt
        <cffile action="write" file="#strPath#test.txt" output="John #chr(32)##chr(32)##chr(32)##chr(32)#Doe has too many spaces in his name.">
        <br />
        Text file saved to: #strPath#test.txt
</cfoutput>

Open in new window

0
 
jfreeman2010Author Commented:
ok, thanks LajuanTaylor.  I will work on this.  Thank you very much for all your help.
0
 
jfreeman2010Author Commented:
Start point ....
0
 
LajuanTaylorCommented:
No problem.

BTW, what content manager are you switching to (WordPress, DokuWiki, etc.)?  I was pondering if the tool had a plugin or something for content migration...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 17
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now