Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create document file form database?

Status
Not open for further replies.

cherphas

Programmer
Jun 5, 2002
24
CA
Hello everyone,

Is there a way that I can get data from a database with Coldfusion and display it/ in a .doc or a Microsoft Word file or even in PDF?
 
The DOC file is fairly easy.
You would just need to use the CFCONTENT tag, and define the MIME type.

The entire code (ie - don't include an HTML, HEAD or BODY tag... it'll just confuse things) for the page would be:
Code:
<cfcontent type=&quot;application/msword&quot;>
<cfheader name=&quot;Content-Disposition&quot; value=&quot;filename=whatever.doc&quot;>

<!--- obviously you can use whatever CFML you need to to put values into the variables... queries, loops, whatever --->
<CFSET SomeVariable = &quot;Hello World&quot;>
<CFSET SomeOtherVariable = &quot;This should be a Word document&quot;>

<CFOUTPUT>
#SomeVariable##Chr(13)#
#SomeOtherVariable ##Chr(13)#
</CFOUTPUT>

In IE, the page will usually still open in the browser, but as a Word OLE document (ie - with the Word rulers and toolbars). In Netscape, it will ask you whether you want to open the document or save it to disk. This is where the CFHEADER tag comes in handy... it allows you to define the default filename that it will get saved as.

You can use CFCONTENT to set whatever MIME type you care to (see though the most common uses are Word and Excel docs.

The trick is formatting. You would need to dig into the MSDN documentation on Word to find out how to define bolds, headings, paragraph formatting, etc. For even though the above will open as a Word doc, it will pretty much still just look like ASCII text.

And setting the MIME type to a PDF document doesn't work, unfortunately. But there are several Custom Tags available that will build PDFs on the fly... check out the developer exchange... I think there are a few listed in there.
Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top