In a Windows system, use PHP to generate WORD Documents,
Preparations
First, make sure that a typical WAMP environment has been installed and configured in your Windows system. Since Interop is purely a Windows feature, we will build Apache and PHP on the Windows platform. In this example, I used EasyPHP 14.1, which is easy to install and configure.
Next, we will install Microsoft Office. The version is not strictly required. I am using Office2013 Professional Edition, but any Office version after 2007 should be available.
We then need to ensure that the Library for developing the Interop application (also called PIA) is installed. To ensure this, open the resource manager and find <Windows directory> \ assembly. We will see the installed PIAs branch below:
We can see a Microsoft. Office. Interop. Word entry (which contains underscores ). This is the PIA we will use in this example. Pay special attention to its "name", "version", and "Public Key tag ". We will use them in PHP scripts.
In this directory, we can also see other PIAs for programming (not only PHP, but also VB.net, C #, etc.) (including the entire Office family ).
If the list does not contain the entire Microsoft. Office. Interop package, you can reinstall the Office and install PIA. You can also manually download and install the package. For detailed installation steps, refer to 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 can only be obtained by installing the Office.
Finally, we need to enable php extension php_com_dotnet.dll in the file PHP. ini and restart the server.
Now we can start programming.
HTML form
Because this demo mainly focuses on background processing, we will use a simple HTML form to display the foreground, which looks like this:
We have a text box for entering "Name", a "Gender" radio button group, an "Age" field value control, and a text field to write "Message". Finally, you also need a "Submit" button.
Save the file named "paiindex.html" in the root directory of the VM. In this way, you can directly access the file through a URL, such as http: // test/interop.
Background
The background PHP file is the core part of our discussion. I will first paste the code below, and then explain it step by step.
<?php $inputs = $_POST;$inputs['printdate']=''; // A dummy value to avoid a PHP notice as we don't have "printdate" in the POST variables. $assembly = 'Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c';$class = 'Microsoft.Office.Interop.Word.ApplicationClass'; $w = new DOTNET($assembly, $class);$w->visible = true; $fn = __DIR__ . '\\template.docx'; $d = $w->Documents->Open($fn); echo "Document opened.<br>
After setting the variable $ inputs to get the value passed in the form, we will create a virtual value to store printdate -- we will discuss later why this variable is needed -- now, we can see these four lines of key code:
$assembly = 'Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c';$class = 'Microsoft.Office.Interop.Word.ApplicationClass'; $w = new DOTNET($assembly, $class);$w->visible = true;
The COM operation in PHP requires a class instance to be requested in an assembly. In our case, I am going to operate on Word. If we take into account the code shown in the previous section, we will be able to construct a complete signature Word PIA:
- "Name", "Version", and "Public Key Token" are the information displayed when we browse "c: \ Windows \ assembly ".
- "Cultrue" is always neutrual.
The extension name of the compiled file of the call class is usually ApplicationClass.
By setting the following two steps, we can initialize a word object:
First, the word object can be saved in the background or displayed on the foreground by setting the visible attribute to true.
Then we open the document to be processed and instantiate it as a $ d variable.
Add the document content in the Document Object Based on the html form text. Here you can set some parameters.
The worst way is to hard encode all the content on the php page and add them to the word object. I strongly recommend that you do not use this method for the following reasons:
1. The code is not flexible. Any changes to php content need to be modified again;
2. Violation of the separation of control layer and display layer;
3 if you need to set the format (alignment, Font, style, and so on) of the word content, this method greatly increases the number of lines of code, and it is very troublesome to modify the style programmatically.
Another method is to use "search-Replace ". This built-in function of PHP is very powerful. We can create a Word document and place separators around the placeholder content to be replaced. For example, we create a document that contains the following content:
{{name}}
In PHP, we only need to replace it with the "Name" value obtained from form submission. This method avoids the disadvantages in the first option. We only need to find the correct delimiter. In this example, except that the template used is a Word document, we are more like doing a template rendering.
The third option is my suggestion and an advanced topic in Word. We will use fields to represent placeholders. In our PHP code, we will directly update the fields of the corresponding form values.
This method is flexible and fast, and conforms to the best practices of Word. This also avoids full-text search of files, which helps improve performance. Note that this option has its disadvantages.
In short, Word has never supported naming indexes since its debut. Although we provide a name for the field we created in the Word document, we still need to use a numerical subscript to access each field. This also explains why we need to use the special feature (setupfields) to do the field index of form fields and the ing between names.
To learn how to insert fields in a Word document (Click here to view a custom version), see the related Word help topic and manual. For this demo, we have a document with five MERGEFIELD fields. In addition, we put the documents and PHP scripts in a directory for easy access.
Note that the printdate field does not have a corresponding form field. This is why we need to add a fake printdate as the key in the $ inputs array. Without this key, the script can still be executed, but there will be a prompt indicating that the $ inputs array does not contain the index printdate.
After the field value is updated using form data, the following command is used to print the document:
$d->PrintOut();
The PrintOut method has several optional parameters. Here, we use the simplest format. This will print a copy of the default printer linked to our Windows machine.
We can use PrintPreview for print preview. In a purely automated scenario, of course, we directly use PrintOut for printing.
Before exiting the word application, we still need to wait because printing takes time to completely exit the background. If no delay (3) exists, $ w-> Quit will be executed immediately and the print operation will be terminated immediately.
Finally, we call $ w-> Quit (false) to choose to close the word application by calling our PHP script. The only parameter provided here is used to specify whether we want to save the change before exiting. We did make changes to the document, but we do not want to save them because we want to provide a clean template for input by other users.
After coding, we can load the form page, enter some content and submit the form. The following shows the PHP Script output and updates the Word document:
Improve coding speed and better understand PIA
PHP is a weak language. A com Object is an Object type. In our PHP coding process, we cannot use the code auto-prompt and complete functions in an object, as in a Word application, a document or even a field. We don't know what features it has or what methods it supports.
This will greatly reduce the development speed. To make development faster, first of all, I suggest that we migrate the development function in c # To Our PHP code. I recommend a free C # IDE called "# develop". You can download it here. Compared with VS, I prefer this software because # develop is smaller in size, simpler and faster in response.
C # It's not scary to migrate code to PHP. Let me show you some C # code first:
Copy codeThe Code is as follows: Word. Application w = new Word. Application ();
W. Visible = true;
String path = Application. StartupPath + "\ template.docx ";
Word. Document d = w. Documents. Open (path) as Word. Document;
Word. Fields flds = d. Fields;
Int len = flds. Count;
Foreach (Word. Field f in flds)
{
F. Select ();
Int I = f. Index;
W. Selection. TypeText ("...");
}
We can see that the C # code is exactly the same as the PHP code we previously showed. Since C # is a strong type language, we can see some type conversion statements, and we have to explicitly assign a type to our variables.
With the code type, we can enjoy the automatic prompts and Code Completion functions of the Code, which will greatly improve the development speed.
Another way for us to develop PHP faster is to use the Word macro command. We first record the actions we need to repeat and then use a macro to record them. A macro is actually Visual Basic and can be easily translated into PHP.
Most importantly, the Office PIA official Microsoft documents, especially the namespaces for each Office application, will always be the most important reference we need. The following are three commonly used applications:
- Excel 2013: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel (v = office.15). aspx
- Word 2013: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word (v = office.15). aspx
- PowerPoint2013: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint (v = office.15). aspx
Conclusion
In this article, we demonstrate how to use the php com library and Microsoft Office Interop function to figure out a Word document.
Windows and Office are widely used in our daily life. To be able to know and understand the power of Office or Windows, PHP is also necessary for any programmer who develops PHP on Windows platform.
With the COM Extension of PHP, the door to understanding this combination is opened.
If you are interested in this part of programming, please leave your comments and we will consider writing more articles on this topic. I very much hope that more real-life application development can use this method.