Linux Hosting
NT/2000 Hosting
DYO Internet Access
HTMLez.com
JAVASCRIPTez.com
PHPez.com

Select a Domain
    

Using ASP Code to Write RTF Files - Formatted Print Docs on the Web

Often you want to control the printing of a web document. But HTML formats differently from Microsoft Word or other common formats. For instance, I recently wanted to display 3 columns in a document. HTML would format sorted information as below:

 a                     j                    s
 b                     k                    t
 c                     l                    u
 ------------ Printer Page Break ------------
 d                     m                    v
 e                     n                    w
 f                     o                    x
 ------------ Printer Page Break ------------
 g                     p                    y
 h                     q                    z
 i                     r
        
But I needed the information sorted like this:

 a                     d                    g
 b                     d                    h
 c                     f                    i
 ------------ Printer Page Break ------------
 j                     m                    p
 k                     n                    q
 l                     o                    r
 ------------ Printer Page Break ------------
 s                     v                    y
 t                     w                    z
 u                     x
        
This was a phone list and users didn't want to scroll down several pages three different times. They wanted what we're used to - Human Logic.

So I could buy a PDF conversion utility... Or I could simply tell everyone that That's life folks! Or I could program it myself. I tried using arrays and counting which failed because different printers had different page lengths. I could have programmed using HP printer language but that requires full HP compatability and we had several kinds of printers.

So, I decided to use rtf. I still used counters but using rtf I could force a page break where I wanted the page break. How do I then program in rtf? It's not simple like HTML. I looked at this url:

http://msdn.microsoft.com/library/?url=/library/en-us/dnrtfspec/html/rtfspec.asp?frame=true
That would be a lot of work to understand all of this. Our old phone list had been stored in Microsoft Word format so I opened it in Word and saved it in RTF format. Then I looked as the raw file Homesite. I could recognize some of the tags from the documentation and looked for where my words began. I decided that everything before that was the required header setting for the rtf document. So I copied that section and saved it separately. Then I looked for what separated the columns. I found
  • \tab
  • \par
  • \page
I also looked in the heading and bound where the date and time were displayed. I replaced each of these with & date() & or & time(). So near the top of my .asp code I placed this code.

I used most of the counting code that I'd earlier created for HTML display and replace response.write code with code placing the output into arrays, one array for each of three columns. After 54 entries in each column I then wrote them to a file. Here's the function:


Function printPage()
      emptyCol3 = "n"
      for j = 1 to 54
                      objNewFile.write(col1(j))
                      col1(j) = " "
                      objNewFile.write(col2(j))
                      col2(j) = " "
                      objNewFile.write(col3(j))
                      col3(j) = " \par "
      next 
      objNewFile.write(" \page ")
      Page1 = 0
      curCol = 0
End Function
When finished processing the Phone List database, I ended with the following code:
objNewFile.write(" }}") 'this is how all rtf files end
objNewFile.close
response.redirect
    ("http://www.jerviswebb.com/george1/rtf/phonelist.rtf")
I could have written directly using

      Response.ContentType = "application/rtf"

but elected instead to physically write the file and then redirect. I haven't shown much .asp code because the code could have been .php or anything else. The principal of this story is that it didn't take much to produce .rtf output. I simply needed

  1. The header portion of the .rtf file. What comes befour your first words is the header.
  2. The rtf tags for tab, new line, and new page.
  3. The closing }}.
The rest was ordinary scripting. The output might not look write in your browser but if you can print rtf files then the printed output should be correct.

cover
Average Customer Review:
February 2002
http://www.wrox.com

cover
Average Customer Review:
February 2002
http://www.wrox.com