随时生成 Word 文档
时间:2005-09-16 来源:r00tkit
Creating Word Documents on the Fly
[color="#ff6600"]Khairul Amri Yunus
IntroductionPrinting 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.
Creating Word Documents on the Fly
[color="#ff6600"]Khairul Amri Yunus
IntroductionPrinting 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.OverviewLets 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.
[color="#ff6600"]Khairul Amri Yunus
IntroductionPrinting 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.
Creating Word Documents on the Fly
[color="#ff6600"]Khairul Amri Yunus
IntroductionPrinting 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.OverviewLets start with the big picture. This is how it works:
- 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 TemplateThis 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.Substituting with the actual valueThe following code opens the specified MS Word template document, substitutes the value and saves it as a new file. We defined a Bookmark named "TODAYDATE" in the document, which will be replaced with today's date.[color="#000000"]
[color="#ff8000"]//1. Instanciate Word
[color="#0000bb"]$word [color="#007700"]= new [color="#0000bb"]COM[color="#007700"]([color="#dd0000"]"word.application"[color="#007700"]) or die([color="#dd0000"]"Unable to instantiate Word"[color="#007700"]);
[color="#ff8000"]//2. specify the MS Word template document (with Bookmark TODAYDATE inside)
[color="#0000bb"]$template_file [color="#007700"]= [color="#dd0000"]"C:/reminder.doc"[color="#007700"];
[color="#ff8000"]//3. open the template document
[color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]Documents[color="#007700"]->[color="#0000bb"]Open[color="#007700"]([color="#0000bb"]$template_file[color="#007700"]);
[color="#ff8000"]//4. get the current date MM/DD/YYYY
[color="#0000bb"]$current_date [color="#007700"]= [color="#0000bb"]date[color="#007700"]([color="#dd0000"]"m/d/Y"[color="#007700"]);
[color="#ff8000"]//5. get the bookmark and create a new MS Word Range (to enable text substitution)
[color="#0000bb"]$bookmarkname [color="#007700"]= [color="#dd0000"]"TODAYDATE"[color="#007700"];
[color="#0000bb"]$objBookmark [color="#007700"]= [color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]ActiveDocument[color="#007700"]->[color="#0000bb"]Bookmarks[color="#007700"]([color="#0000bb"]$bookmarkname[color="#007700"]);
[color="#0000bb"]$range [color="#007700"]= [color="#0000bb"]$objBookmark[color="#007700"]->[color="#0000bb"]Range[color="#007700"];
[color="#ff8000"]//6. now substitute the bookmark with actual value
[color="#0000bb"]$range[color="#007700"]->[color="#0000bb"]Text [color="#007700"]= [color="#0000bb"]$current_date[color="#007700"];
[color="#ff8000"]//7. save the template as a new document (c:/reminder_new.doc)
[color="#0000bb"]$new_file [color="#007700"]= [color="#dd0000"]"c:/reminder_new.doc"[color="#007700"];
[color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]Documents[color="#007700"][[color="#0000bb"]1[color="#007700"]]->[color="#0000bb"]SaveAs[color="#007700"]([color="#0000bb"]$new_file[color="#007700"]);
[color="#ff8000"]//8. free the object
[color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]Quit[color="#007700"]();
[color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]Release[color="#007700"]();
[color="#0000bb"]$word [color="#007700"]= [color="#0000bb"]null[color="#007700"];
[color="#0000bb"]?>
That's it. Open the new document (c:/reminder_new.doc) and you will see today's date at the bookmark's location. - Initially we instantiated the Word object using this code:
[color="#000000"]
[color="#007700"]);
[color="#0000bb"]?>
- Then we specified the template document that contains the sample output and the Bookmark TODAYDATE:
[color="#000000"]
[color="#007700"];
[color="#0000bb"]?>
- Next we opened the document:
[color="#000000"]
[color="#0000bb"]Documents[color="#007700"]->[color="#0000bb"]Open[color="#007700"]([color="#0000bb"]$template_file[color="#007700"]);
[color="#0000bb"]?>
- Then we got today's date using date() function. The Bookmark TODAYDATE will be replaced with this value:
[color="#000000"]
[color="#007700"]);
[color="#0000bb"]?>
- Next we found the Bookmark in the document and created a new MS Word Range. Range is used to perform text substitution or insertion.
[color="#000000"]
[color="#007700"];
[color="#0000bb"]$objBookmark [color="#007700"]= [color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]ActiveDocument[color="#007700"]->[color="#0000bb"]Bookmarks[color="#007700"]([color="#0000bb"]$bookmarkname[color="#007700"]);
[color="#0000bb"]$range [color="#007700"]= [color="#0000bb"]$objBookmark[color="#007700"]->[color="#0000bb"]Range[color="#007700"];
[color="#0000bb"]?>
- Then we substituted the bookmark with actual value:
[color="#000000"]
[color="#0000bb"]Text [color="#007700"]= [color="#0000bb"]$current_date[color="#007700"];
[color="#0000bb"]?>
- Saved the template as a new document (c:/reminder_new.doc)
[color="#000000"]
[color="#007700"];
[color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]Documents[color="#007700"][[color="#0000bb"]1[color="#007700"]]->[color="#0000bb"]SaveAs[color="#007700"]([color="#0000bb"]$new_file[color="#007700"]);
[color="#0000bb"]?>
- Finally, we free the object:
[color="#000000"]
[color="#0000bb"]Quit[color="#007700"]();
[color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]Release[color="#007700"]();
[color="#0000bb"]$word [color="#007700"]= [color="#0000bb"]null[color="#007700"];
[color="#0000bb"]?>
Improving the codeThe above code will return an error if the specified bookmark is not found in the document. To check whether the bookmark exists in the template document, use this:[color="#000000"]
[color="#0000bb"]ActiveDocument[color="#007700"]->[color="#0000bb"]Bookmarks[color="#007700"]->[color="#0000bb"]Exists[color="#007700"]([color="#0000bb"]$bookmarkname[color="#007700"]))
{
[color="#ff8000"]//then create a Range and perform the substitution
[color="#0000bb"]$objBookmark [color="#007700"]= [color="#0000bb"]$word[color="#007700"]->[color="#0000bb"]ActiveDocument[color="#007700"]->[color="#0000bb"]Bookmarks[color="#007700"]([color="#0000bb"]$bookmarkname[color="#007700"]);
[color="#0000bb"]$range [color="#007700"]= [color="#0000bb"]$objBookmark[color="#007700"]->[color="#0000bb"]Range[color="#007700"];
[color="#ff8000"]//now substitute the bookmark with actual value
[color="#0000bb"]$range[color="#007700"]->[color="#0000bb"]Text [color="#007700"]= [color="#0000bb"]$current_date[color="#007700"];
}
[color="#0000bb"]?>
The approach above replaces Bookmarks with the actual value, therefore the word highlighted and defined as the bookmark will be deleted. If you want to insert text before the bookmark, use the following:[color="#000000"]
[color="#0000bb"]InsertBefore[color="#007700"]([color="#dd0000"]"Today's date is: "[color="#007700"]);
[color="#0000bb"]?>
Alternatively, to insert text after the bookmark, use this:[color="#000000"]
[color="#0000bb"]InsertAfter[color="#007700"]([color="#dd0000"]"Have a nice day!"[color="#007700"]);
[color="#0000bb"]?>
To select all of the text within the template document, use the following:[color="#000000"]
[color="#0000bb"]ActiveDocument[color="#007700"]->[color="#0000bb"]Content[color="#007700"]->[color="#0000bb"]Text[color="#007700"];
[color="#0000bb"]?>
To automatically load the document, use header functions (add this to the end of the PHP file):[color="#000000"]
[color="#007700"]);
[color="#0000bb"]header[color="#007700"]([color="#dd0000"]"Content-Disposition: attachment; filename="Reminder New.doc""[color="#007700"]);
[color="#0000bb"]readfile[color="#007700"]([color="#0000bb"]$new_file[color="#007700"]);
[color="#0000bb"]?>
Alternatively, to prompt Open/Save dialog box, use:[color="#000000"]
[color="#007700"]);
[color="#0000bb"]header[color="#007700"]([color="#dd0000"]"Content-Disposition: attachment; filename="Reminder New.doc""[color="#007700"]);
[color="#0000bb"]header[color="#007700"]([color="#dd0000"]'Cache-Control: must-revalidate, post-check=0, pre-check=0'[color="#007700"]);
[color="#0000bb"]readfile[color="#007700"]([color="#0000bb"]$new_file[color="#007700"]);
[color="#0000bb"]?>
Note that the above two examples may not work on certain browsers and/or versions.You may want to terminate/kill WINWORD.EXE instance if the code triggers an error or terminates unexpectedly. Run Windows Task Manager and end WINWORD.EXE instance.That's it. Feel free to share your thoughts and improved versions of the above code. Khairul Amri Yunus
Kometsoft (M) Sdn. Bhd.
相关阅读 更多 +