|
Creating Word Documents on the Fly
Khairul Amri Yunus
Introduction
Printing HTML pages directly from browsers usually does not give the best printing
output, compared to output from word processors such as Microsoft Word. For
web-based systems that generate letters, reports and documents, an output that
looks authentic and original is very important. Furthermore, printing output
directly from browsers is usually dependant on the browser's configuration. Therefore
web-based applications may face difficulties in giving the best printing output
especially when dealing with large user base.
This article provides a guideline on how to generate documents and letters in
MS Word format using a pre-defined template. My customer (a government agency)
required for Reminder Letters to be generated automatically in MS Word format
from the Loan Management System that I delivered to them. Using this approach,
they can generate a Reminder Letter for each loan which then can be printed
nicely and saved for future reference.
This solution can be accomplished using COM extension. Please note that the
server running this code MUST have MS Word installed.
Overview
Lets start with the big picture. This is how it works:
- First, we need to prepare a MS Word document (*.doc) that contains the sample output. The content of the Reminder Letter is normally the same, except for, in each case we need to put the Applicant Name, Loan Reference Number and Today's date in the letter to be generated.
- In this MS Word template document, we need to define places where PHP should insert the actual value (such as one pulled from database or evaluated from code). This can be done using the "Bookmark" feature in MS Word documents.
- Our PHP code will substitute the Bookmark with the actual value.
- And finally, we save it as a new document.
Preparing the MS Word Template
This step is actually simple, all we need is a MS Word document (*.doc) that
contains the sample output and Bookmarks.
To create a Bookmark in a MS Word document, click on Insert/Bookmark. You can
highlight a word or sentence and make them a Bookmark. In this code, we will
substitute the Bookmark with the actual value, therefore the word you highlighted
and saved as a Bookmark will be substituted.
A Bookmark name can only contain letters, numbers and underscores. To see all of
bookmarks in the document, click on Tools/Options and check Bookmarks under Show
group in the View tab.
The code we use in the next section just substitutes a specified bookmark with text,
therefore the formatting, images, tables and etc will not be lost.
[ Next Page ]
| Comments: | ||
| Word on server vs. mime-type tricks | Steve | 09/18/07 10:00 |
| Fatal error: com class not defined | Hans | 07/08/07 16:31 |
| how to instantiate COM | mienard lumad | 06/01/07 03:51 |
| write word script | Sandr | 05/25/07 17:17 |
| Inserting Line Breaks | Jon | 01/14/06 08:19 |
| Getting Error | swetha | 10/28/05 06:13 |
| RE: Pulling out MS Word document properties | Feierabendbier | 07/25/05 06:39 |
| create word 2000 file | dinanath | 07/22/05 08:01 |
| Anders Nielsen - RE: A better solution... | Object OR .COM | 07/15/05 17:57 |
| A better solution... | Anders Nielsen | 02/20/05 00:37 |
| RE: No Word on Server | Steven Vanderhoydonks | 02/08/05 04:32 |
| ideas on parsing | Dean | 01/26/05 12:16 |
| Pulling out MS Word document properties | Rahul | 01/04/05 19:19 |
| display a word document in html format with p | Deepthi Valmikam | 11/28/04 02:21 |
| generat .doc with tables? | anna | 11/18/04 09:26 |
| RE: Are there other options? | Cristian Muraru | 06/01/04 08:43 |
| RE: Are there other options? | tibim | 03/02/04 15:50 |
| import xhtml | nick | 02/26/04 17:09 |
| why bother? | madis | 01/04/04 10:40 |
| RE: Are there other options? | Mr. Mechano | 12/30/03 04:51 |
| There is a better RTF-DOC-PDF example | Jon Ritchey | 12/29/03 14:15 |
| PDF v. WORD | sep | 12/23/03 16:33 |
| RE: Are there other options? | Khairul Amri Yunus | 12/20/03 03:30 |
| RE: Are there other options? | Yosh | 12/13/03 16:05 |
| RE: Word on Server - Not recommended at all | Alex | 12/11/03 16:03 |
| Not as bad as it sounds.... | andrew taylor | 12/10/03 06:26 |
| Templates in RTF | Jeroen Keppens | 12/09/03 03:42 |
| RE: Are there other options? | Eric Esquibel | 12/08/03 17:42 |
| poor choice.. | Paul | 12/06/03 18:46 |
| PDF a better choice for printing | Drew F | 12/05/03 12:05 |
| example using rtf... | jon roig | 12/05/03 11:59 |
| RE: doc poor choice for printing | nightowl | 12/05/03 06:00 |
| RE: Are there other options? | nightowl | 12/05/03 05:58 |
| No Word on Server | Martin Roesch | 12/04/03 07:38 |
| ex vb programmer? | Joe PHP | 12/03/03 16:28 |
| doc poor choice for printing | johnnyv | 12/02/03 16:31 |
| Are there other options? | Luiz Guilherme | 12/02/03 14:22 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


