Preparation First, please ensure that a typical WAMP environment has been installed and configured on your Windows system. Since Interop is purely a Windows feature, we will build Apache and PHP under the Windows platform. In this example, I used EasyPHP 14.1, which is very easy to install and configure. Next, we want to install Microsoft Office. The version is not strictly required. I'm using Office 2013 Professional, but any version of Office after 2007 should work. We then need to make sure that the libraries for developing Interop applications (also known as PIA, Priority Interaction Components) are installed. To ensure this, we can open the explorer and find We can see a Microsoft.Office.Interop.Word entry (underlined in this screenshot). This is the PIA we will be using in this example. Please pay special attention to its "name", "version" and "public key token". We are going to use them in our PHP script. In this directory, we can also see other PIAs (including the entire Office family) used for programming (not only PHP, but also VB.net, C#, etc.). If this list does not contain the entire package of Microsoft.Office.Interop, we can reinstall Office and include PIA in the installation; we can also manually download and install this package. Detailed installation steps can be found on this MSDN page. Note: Only Microsoft Office 2010 PIA Redistributable can be downloaded and installed separately. The PIA version in this package is 14.0.0. Version 15 is only available by installing Office. Finally, we need to enable the PHP extension php_com_dotnet.dll in the file php.ini and restart the server. Now we can start programming. HTML Form Since this demo mainly focuses on backend processing, we use a simple HTML form to display the frontend. It should look like this: We have a text box for entering "Name", a radio button group for "Gender", a field value control for "Age" and a text field for writing "Message". Finally, we need a "Submit" button. Name the file "index.html" and save it in the root directory of the virtual host so that we can access the file directly through the URL, for example: http://test/test/interop Backstage The backend PHP file is the core part we are going to discuss. I will post the code below first, and then explain it step by step
After setting up the variable $inputs to get the value passed in the form, we need to create a dummy value to store the printdate - we will discuss why this variable is needed later - now, we see these 4 lines The more critical code:
COM manipulation in PHP requires requesting an instance of a class in an assembly. In our case, we will be working with Word. If we take into account the code shown in our last screenshot, we will be able to construct a fully signed Word PIA:
By setting the following two steps, we can initialize a word object: First of all, the word object can be saved in the background or displayed in the foreground by setting the visible attribute to true. Then, we open the document to be processed and instantiate it as a $d variable. In the document object, add the content of the document based on the text of the html form. Some parameters can be set here. 1 The code is not flexible, any changes in the PHP content require re-modification of the script;
In PHP, we simply replace with the "Name" value obtained from the form submission. This approach avoids the disadvantages of the first option. We just need to find the correct delimiter, and in this example, except that the template used is a word document, we are more like doing a template rendering.
This method is flexible, fast, and conforms to Word’s best practices. This also avoids full-text searching of the file, which helps improve performance. Please note that this option has its drawbacks too. In short, since its debut, Word has never supported named indexed fields. Although we provide a name for the fields we create in the Word document, we still use numeric subscripts to access each field. This also explains why we need to use a dedicated function (setupfields) to do the mapping manual between the field index and name of the form field
Please note that the printdate field does not have a corresponding form field. This is why we add a fake printdate as a key in the $inputs array. Without this key, the script can still be executed, but there will be a prompt indicating that the index printdate does not exist in the $inputs array. After updating the field value using form data, we will print the document using the following command:
PrintOut method has several optional parameters, here, we use the simplest format. This will print a copy to the default printer linked to our Windows machine.
We also need to wait a moment before exiting the word application, because the printing job takes time to completely exit the background. Without delay(3), $w->Quit will be executed immediately and the printing job will be terminated immediately. Finally, we call $w->Quit(false) to choose to close the word application via our PHP script call. The only parameter provided here is to indicate whether we want to save changes before exiting. We do make changes to the document, but we don't want to save them because we want to have a clean template for other users' input. When we finish coding, we can load the form page, enter some content and submit the form. The screenshot below shows the output of the PHP script while updating the Word document: Increase coding speed and better understand PIA PHP is a weakly typed language. A COM object is an Object type. In our PHP coding process, we cannot use the code auto-suggestion and completion function in an object, the same is true in a Word application, a document or even a field. We don't know what features it has, or what methods it supports.
Migrating C# code to PHP is not scary at all. Let me show you some C# code first: We can see that the C# code is exactly the same as the PHP code base we showed before. Since C# is a strongly typed language, we can see some type conversion statements and we have to explicitly assign a type to our variables. With code types, we can enjoy the automatic prompting and automatic code completion functions of the code, so that our development speed will be greatly improved.
The most important thing is that the official Microsoft documentation of Office PIA, especially the namespace of each Office application in the document, will always be the reference we need most. The three most commonly used applications are as follows:
Conclusion In this article, we demonstrate how to use the PHP COM library and Microsoft Office Interop functionality to edit a Word document. Windows and Office can be said to be widely used in our daily lives. Being able to know and understand the power of Office or Windows and PHP is very necessary for any programmer who develops PHP on the Windows platform. Using PHP's COM extension, the door to mastering this combination is opened. If you are interested in this part of programming, please leave your comment and we will consider writing more articles on this topic. I really look forward to more real-life application development using this approach. |