Copy link to clipboard
Copied
Hi all
I am using Coldfusion 7.
I have a .rtf file as a template, with various placeholders placed throughout. I use cffile to read this template. I then have a query that replaces the placeholders with data. I then attempt to invoke MS Word to save the file as a .doc file. The problem is I only get garbage code once I execute this. I believe I am looking at the actual text data of the .rtf file, and Word is never invoked.
<!--- Read template into variable "rtf" --->
<cffile action="read" file="#fileLocation##fileName#" variable="rtf">
<!--- Replace placeholders with data --->
<cfset rtf = replace(rtf, "%InvoiceNumber%", trim(invoice), "ALL")>
blah blah blah.......
<!--- Suggest default filename for document --->
<cfheader name="Content-Disposition" value="filename=INVOICE.doc">
<!--- Set the content type so Word is invoked --->
<cfcontent type="application/msword">
<cfoutput>#rtf#</cfoutput>
Any suggestions? Thanks in advance.
-J
Copy link to clipboard
Copied
Part of your problem may be that the MIME type and file content don't match. You are telling the brower to expect a Word document, but you are sending an RTF document. Try using the MIME type application/rtf. You will need to depend on the user saving the RTF file to Word format.
You might try something like the sample below which I have *not* tested. This would replace the cfcontent and cfoutput block in the code you posted. This *should* output the RTF document to the client.
<cfheader name="Content-Disposition" value="attachment;filename=myfile.rtf">
<cfcontent type="application/rtf" variable="#ToBinary(ToBase64(rtf))#" reset="yes">
Question:
When you say "I then attempt to invoke MS Word to save the file as a .doc file." do you mean on the client side or server side?
Copy link to clipboard
Copied
JR thanks for your response. I will have to try this out.
I am talking about client side.
Copy link to clipboard
Copied
This did seem to work.
However I have a lot of data and this will be on a loop with about a dozen files to save, so it's not reasonable to have the user hit "Save As" for 12 files. I am trying to find a way to simply save them as .doc's (or maybe .rtf's) without having to prompt the user to save.
I may go the PDF route and just load the files that way. Thanks again for your help and if you think of anything, please let me know.
-J
Copy link to clipboard
Copied
How about creating the documents, using CFZIP to create an archive them pushing that to them?
Copy link to clipboard
Copied
If you want to create and save RTF files on the server replace the CFCONTENT / CFOUTPUT section with a CFFILE call to create a binary file on the server.
If you want to create Word files on the server with CF7 you'll probably need to find a third-party Java component and invoke it from your CF code.
You may want to reconsider your workflow to reduce the amount of clicking the end user has to do. You might for example allow the user to fill out a form to request the files, then use CFSCHEDULE to check for requests, create the files, then email them.
Copy link to clipboard
Copied
Very helpful answers all. Thanks for the input.
PS Sorry I tried to give "helpful answers" to everyone but it cut me off at 2 - new to this forum.
Copy link to clipboard
Copied
I seem to have stumbled upon a method that works. Is this method not wise?
I use the .rtf template using cffile and fill in the placeholders with data as before.
Instead, however, I save the file using cffile with a .doc extension. The file opens in Word perfectly (Compatability Mode). I even have a .jpg logo and it shows up perfectly.
<!--- Read template into variable "rtf" --->
<cffile action="read" file="#fileLocation#template.rtf" variable="rtf">
<!--- Replace placeholders with data --->
<cfset rtf = replace(rtf, "%InvoiceNumber%", trim(invoice), "ALL")>
blah blah blah....
<!--- create/append to text file --->
<cffile action="Append" file="#fileLocation#INVOICE.doc"
output="#rtf#">
I can then put this in a loop, use variables in the file name, and it seems like I have something that works. Are there any problems this way? Thanks again.
-J
Copy link to clipboard
Copied
There are two possible issues:
1) I have run into an issue using Excel that might be relavent. If I open an HTML file (which has a .xls file extension) containing an HTML table in Excel 2000 the MS Excel application is able to figure out that the table should be rendered as a spreadsheet and displays accordingly. If I open the same file in Excel 2010 I receive a warnining because the file extension .xls does not match the file's contents which are HTML and not a binary Excel file. A similar problem may affect opening RTF files using a .doc file extension. You should test the results in various versions of Word 2003, 2007, 2010.
2) Other consumers of these files may expect them to be Word formatted files, not RTF files, based on the .doc file extension. A problem might occur if a consumer tries to use an API or application to work with your file if this API or application is not compatible with RTF files.
In order to avoid either problem I recommend that you use the .rtf file extension for your RTF files.