Trading Systems Analyst (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Creating Word Documents on the Fly
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:
  1. 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.
  2. 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.
  3. Our PHP code will substitute the Bookmark with the actual value.
  4. 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:
thank youFra Orv01/27/09 07:39
RE: Inserting Line Breaksbobby.tables10/28/08 07:55
Word on server vs. mime-type tricksSteve09/18/07 10:00
Fatal error: com class not definedHans07/08/07 16:31
how to instantiate COMmienard lumad06/01/07 03:51
write word scriptSandr05/25/07 17:17
Inserting Line BreaksJon01/14/06 08:19
Getting Errorswetha10/28/05 06:13
RE: Pulling out MS Word document propertiesFeierabendbier07/25/05 06:39
create word 2000 filedinanath07/22/05 08:01
Anders Nielsen - RE: A better solution...Object OR .COM07/15/05 17:57
A better solution...Anders Nielsen02/20/05 00:37
RE: No Word on ServerSteven Vanderhoydonks02/08/05 04:32
ideas on parsing Dean01/26/05 12:16
Pulling out MS Word document propertiesRahul01/04/05 19:19
display a word document in html format with pDeepthi Valmikam11/28/04 02:21
generat .doc with tables?anna11/18/04 09:26
RE: Are there other options?Cristian Muraru06/01/04 08:43
RE: Are there other options?tibim03/02/04 15:50
import xhtmlnick02/26/04 17:09
why bother?madis01/04/04 10:40
RE: Are there other options?Mr. Mechano12/30/03 04:51
There is a better RTF-DOC-PDF exampleJon Ritchey12/29/03 14:15
PDF v. WORDsep12/23/03 16:33
RE: Are there other options?Khairul Amri Yunus12/20/03 03:30
RE: Are there other options?Yosh12/13/03 16:05
RE: Word on Server - Not recommended at allAlex12/11/03 16:03
Not as bad as it sounds....andrew taylor12/10/03 06:26
Templates in RTFJeroen Keppens12/09/03 03:42
RE: Are there other options?Eric Esquibel12/08/03 17:42
poor choice..Paul12/06/03 18:46
PDF a better choice for printingDrew F12/05/03 12:05
example using rtf... jon roig12/05/03 11:59
RE: doc poor choice for printingnightowl12/05/03 06:00
RE: Are there other options?nightowl12/05/03 05:58
No Word on ServerMartin Roesch12/04/03 07:38
ex vb programmer?Joe PHP12/03/03 16:28
doc poor choice for printingjohnnyv12/02/03 16:31
Are there other options?Luiz Guilherme12/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.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.