Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

How To Output RTF-formatted Documents with ASP

HTML isn't your only choice for displaying content from ASP. Learn how to build a simple class to generate RTF documents and tables on the fly with basic text and paragraph formatting.


advertisement
SP is a great technology for delivering custom HTML-based content to a Web page. You can also use ASP to create output for print and distribution beyond Web pages. In fact, ASP can be a great medium for creating complex reports in a variety of formats. This article shows you how to use ASP to generate RTF files, and gives you some simple functions for formatting blocks of text and paragraphs as well as a function to produce an RTF table from a SQL query. The ASP makeTable function produces a generic table containing data from a query that you can then format as needed in your favorite word processing application.

Why RTF?
RTF (Rich Text Format) is designed to be platform-independent without losing detailed formatting. According to Microsoft,

"The RTF Specification provides a format for text and graphics interchange that can be used with different output devices, operating environments, and operating systems. RTF uses the American National Standards Institute (ANSI), PC-8, Macintosh, or IBM PC character set to control the representation and formatting of a document, both on the screen and in print. With the RTF Specification, documents created under different operating systems and with different software applications can be transferred between those operating systems and applications."
Obviously, one advantage of outputting to RTF is platform-independence. Perhaps a more important advantage for some developers is that the ASP-to-RTF output code does not require any special configuration on the hosting Web server beyond permission to use the FileSystemObject in a specific location. Outputting a Microsoft Word file, for example, requires the hosting Web server to have Word installed and an requires an administrator to configure the server permissions so the ASP engine can run it. Unfortunately, running Word incurs server overhead (especially when multiple instances of Word are open and unused instances aren't closed appropriately). In addition, using Word automation creates another potential security problem: Word is VBA-aware. Running Word on a Web server remotely can expose the server to embedded macros.



In contrast, RTF is a publicly available format specification; therefore, you can create RTF output using simple text processing, and avoid these issues. You can think of RTF as just another markup language, much like HTML. The only difference is the destination of RTF output is to a file, not a Web page.

Implementing RTF Output: The Basics
To write an RTF file, you can use the FileSystemObject from an ASP page using code such as this:

      Dim fso, MyFile, sFileName
      Set fso = CreateObject("Scripting.FileSystemObject")
      sFileName = "OutputToRTF.rtf"
      Set MyFile = fso.CreateTextFile(Server.MapPath(".") & "\" & sFileName, True)

This code should be easy to understand. The sFileName variable points to an RTF file, which needs to be installed somewhere in the Web server directory structure. The Set MyFile method in this code uses Server.MapPath rather than a literal path to locate the target file directory. You can use a literal path if you like.

Author's Note: Important! If your server file system is NTFS you must set permissions so the ASP account has read/write access to the file (OutputToRTF.rtf in the preceding example code). Whenever you use the FileSystemObject to write to a file on the server, pay attention to any security issues involved.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap