Generate dynamic RTF (Rich Text Format) documents easily

AID: 4421
  • Status: Published

2200 points

  • ByNurAzije
  • TypeTutorial
  • Posted on2011-01-27 at 04:53:32

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.

 

rtf-word.png
  • 117 KB
  • Create an RTF file
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:

 

access.png
  • 133 KB
  • Create an Access DB
Create an Access DB



ID – autonumaber and primary key
Fname – text
Lname – text
Email – text
Tel – text
Address – memo
                                    
1:
2:
3:
4:
5:
6:

Select allOpen 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>
                                    
1:

Select allOpen 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]
                                    
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen 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
                                    
1:

Select allOpen 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.

    Asked On
    2011-01-27 at 04:53:32ID4421
    Tags

    RTF

    ,

    Rich Text Format

    ,

    dynamic documents

    ,

    WhizBase

    ,

    MS Access

    Topic

    Scripting Languages

    Views
    1519

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Loading Advertisement...

    Top Scripting Languages Experts

    1. mplungjan

      26,975

      0 points yesterday

      Profile
      Rank: Genius
    2. dgofman

      21,600

      1,500 points yesterday

      Profile
      Rank: Genius
    3. billprew

      21,340

      2,800 points yesterday

      Profile
      Rank: Genius
    4. RobSampson

      19,400

      0 points yesterday

      Profile
      Rank: Genius
    5. chaituu

      17,100

      0 points yesterday

      Profile
      Rank: Sage
    6. leakim971

      17,100

      0 points yesterday

      Profile
      Rank: Genius
    7. COBOLdinosaur

      11,175

      0 points yesterday

      Profile
      Rank: Genius
    8. Ray_Paseur

      10,486

      0 points yesterday

      Profile
      Rank: Savant
    9. nap0leon

      10,300

      0 points yesterday

      Profile
      Rank: Wizard
    10. Qlemo

      9,740

      0 points yesterday

      Profile
      Rank: Genius
    11. farzanj

      8,150

      0 points yesterday

      Profile
      Rank: Genius
    12. KenMcF

      7,000

      0 points yesterday

      Profile
      Rank: Genius
    13. StingRaY

      6,000

      0 points yesterday

      Profile
      Rank: Wizard
    14. Roonaan

      6,000

      2,000 points yesterday

      Profile
      Rank: Genius
    15. TommySzalapski

      6,000

      0 points yesterday

      Profile
      Rank: Genius
    16. selvol

      5,500

      0 points yesterday

      Profile
      Rank: Wizard
    17. DaveBaldwin

      5,300

      0 points yesterday

      Profile
      Rank: Genius
    18. Chris-Dent

      4,800

      0 points yesterday

      Profile
      Rank: Genius
    19. tagit

      4,800

      0 points yesterday

      Profile
      Rank: Genius
    20. maeltar

      4,664

      0 points yesterday

      Profile
      Rank: Guru
    21. ahoffmann

      4,600

      0 points yesterday

      Profile
      Rank: Genius
    22. JESii

      4,300

      0 points yesterday

      Profile
      Rank: Wizard
    23. Sudhakar-Pulivarthi

      4,268

      0 points yesterday

      Profile
      Rank: Guru
    24. xterm

      4,200

      0 points yesterday

      Profile
      Rank: Sage
    25. CyanBlue

      4,100

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame