Generate dynamic RTF (Rich Text Format) documents easily

Published:
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access.

In this tutorial I will show you how to make RTFs with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html

Create your document
First you need to make an RTF template, it is how the document will look like. I have created a file which lists the first name, last name, telephone, email and address in a table.

 Create an RTF file
As you can see the dynamic part will contain WhizBase code.  I have putted $wbf[fname], $wbf[lname], $wbf[email], $wbf[tel] and $wbf[address] in the cells I want to show dynamic data.

These placeholders will be replaced dynamically with data.

Now just save the document as RTF file. You go to "Save As" and save the file as "form.rtf".



Create you database file

The database can be in any format, I will use Microsoft Access because it is the easiest for me. You can use MySQL, Oracle, MS SQL or even Microsoft Excel.

I will create a database file with one table "contacts", it will contain these 6 fields:

 Create an Access DB
ID – autonumaber and primary key
                      Fname – text
                      Lname – text
                      Email – text
                      Tel – text
                      Address – memo

Open in new window


Now save this file as "form.mdb"
I have inserted some records in this table.

Create WhizBase file
Finally we need a file which will be our webpage which takes the ID and gives us the RTF document. So we need to implement an input form for ID, and when submit is clicked then start downloading the new generated file.

First let's rename the file "form.rtf" to "form.ic", "ic" is the extension for included files in WhizBase by the function $wbrinc.

Now create an HTML file with this code:
<html><body><form action='form.wbsp' method='post'>Insert ID: <input type='text' name='wbf_id' /><input type='submit' value='Export file' /></form></body></html>

Open in new window


This is a very basic HTML form file, the only thing you must not forget is the name of the text field, it will be wbf_id, which I have already explained in previous tutorials. Save this file as "default.wbsp".

Now we create another WhizBase file where we query the DB and include the RTF template file.

[FormFields]
                      WB_BaseName=form.mdb
                      WB_Command=Q
                      WB_RcdSet=contacts
                      WB_ShowLogo=F
                      wb_contenttype=application/msword
                      <!--WB_BeginTemplate-->
                      $wbrinc[form.ic]

Open in new window


As you can see nothing new, we are simply querying the DB and including the template which have the placeholders which will be replaced with data. The only important and new thing is the line:
wb_contenttype=application/msword

Open in new window


Here we say to the browser that this documents type is not HTML so do not render it, we say it is MS WORD  application type, so any popular browser will just download it to your computer.

And you have now a ready script to make RTF reports easily.

For more information please visit my member profile at http://www.experts-exchange.com/M_5123186.html
Or visit the WhizBase official site at www.whizbase.com

NurAzije is a PHP and WhizBase programmer, who at the time of article publication has been working in partnership with WhizBase company on several projects and very recently as an employee.
2
6,454 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.