Tutorial on using PHP to generate Word documents under Windows System _php Tutorial

Source: Internet
Author: User
Tags dotnet

A tutorial that uses PHP to generate Word documents under Windows systems,


Preparatory work

First, make sure that a typical WAMP environment is installed and configured on your Windows system. Since interop is purely a feature of Windows, we will build Apache and PHP under the Windows platform. In this example, I used the easyphp 14.1, which is easy to install and configure.

Next, we want to install Microsoft Office. Versions are not strictly required. I am using the Office2013 Pro version, but any version of office after 2007 should be available.

We then need to ensure that the libraries that develop the interop application (also known as the PIA, priority interaction) are installed. To ensure this, we can open the explorer and find \assembly, and we'll see the PIAs branch installed below:

We can see a Microsoft.Office.Interop.Word entry (underlined in this). This is the PIA we will use in this example. Pay particular attention to its "name", "Version", and "public key token". We're going to be using them in PHP scripts.

In this directory, we can also see other PIAs (including the entire Office family) for programming (not just 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 the PIA in the installation, or we can manually download and install the package. Detailed steps for installation can be found on this MSDN page.

Note: Only Microsoft Office Redistributable PIA can be downloaded separately for installation. The PIA version in this package is 14.0.0. Version 15 can only be obtained by installing Office.

Finally, we need to enable PHP extension Php_com_dotnet.dll in file php.ini and restart the server.

Now we can start programming.

HTML form

Since the demo focuses on dealing with the background, we use a simple HTML form to show the foreground, which should look like this:

We have a text box for entering "Name", a "Gender" radio button Group, a "age" field value control and a text field to write "Message", and finally 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 from the URL, for example: Http://test/test/interop

Background

The PHP file behind the scenes is a central part of what we are going to discuss. I'm going to paste the code down here, and then I'll explain it step-by-step.

<?php $inputs = $_post; $inputs [' PrintDate ']= ';  A dummy value to avoid a PHP notice as we don ' t has "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.
"; $flds = $d->fields; $count = $flds->count;echo "There is $count fields in this document.
"; Echo"
      "; $mapping = Setupfields (); foreach ($flds as $index = + $f) {$f->select (); $key = $mapping [$index]; $value = $inputs [$key]; if ($key = = ' gender ') {if ($value = = ' m ') $value = ' Mr. '; else $value = ' Ms. '; } if ($key = = ' PrintDate ') $value = Date (' y-m-d h:i:s '); $w->selection->typetext ($value); echo "
    • Mappig field $index: $key with value $value
    • ";} echo "
"; echo "Mapping done!
"; echo" Printing. Please wait ...
"; $d->printout (); sleep (3); echo "done!"; $w->quit (false); $w =null; function Setupfields () {$mapping = array (); $mapping [0] = ' gender '; $mapping [1] = ' name '; $mapping [2] = ' age '; $mapping [3] = ' msg '; $mapping [4] = ' printdate '; return $mapping;}

After setting the variable $inputs used to get the value passed in the form, we'll create a dummy value to store the printdate--we'll talk about why we need this variable later--now we see the 4 lines that are more critical:

$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;

COM manipulation in PHP requires an instance of class to be requested in a assembly. In our case, I see that I will be working with Word. If we take into account the code shown in our previous, we will be able to construct a fully signed word PIA:

    • "Name", "Version", "Public Key Token" is the information displayed when we browse "c:\Windows\assembly"
    • "Cultrue" is always neutrual.


The calling class compiles a file suffix that is usually named 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 in the foreground by setting the Visible property to True.

We then open the document that will be processed and instantiate it as a $d variable.

In a Document object, add the contents of the document based on the text of the HTML form, where you can set some parameters.
The worst way to do this is to hardcode all the content on the PHP page and then add them to the Word object. I strongly recommend that this approach not be used for the following reasons:

1 code is not flexible, any changes in PHP content need to re-modify the script;
2 violation of the control layer, the separation of the display layer;
3 If you need to format Word content (alignment, font, style, etc.), this way greatly increases the number of lines of code, and programmatically modifying the style is cumbersome.


Another way is to use search-replace. This is a very powerful feature built into PHP. We can create a Word document that places some delimiters around the placeholder content that needs to be replaced. For example, we create a document that contains the following:

{{Name}}

In PHP, we simply replace it 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, in this case, in addition to using the template is a Word document, we are more like a template rendering.


The third option is my recommendation and is a high-level topic in Word. We will use fields to represent placeholders, and in our PHP code we will directly update the fields of the corresponding form values.

This approach is flexible, fast, and conforms to Word's best practices. This also avoids full-text search of files, which helps improve performance. Please note that this option has its drawbacks.

In short, since the debut, Word has never supported a named indexed field. Although we provide a name for the field we created in the Word document, we still have to use a numeric subscript to access each field. This also explains why we use dedicated features (Setupfields) to do the mapping between field indexes and names of form fields.


Learn how to insert a field in a Word document (click here to see a customized version), see the related Word Help topics and manuals. For this demo, we have a document with 5 MERGEFIELD fields. In addition, we put the document and PHP script in a directory for easy access.

Note that the PrintDate field does not have a corresponding form field. That's why we're adding a dummy printdate to the $inputs array as key. Without this key, the script can still be executed, but there are hints that there is no index printdate in the $inputs array.

After updating the value of the field with the form data, we will print the document using the following command:

$d->printout ();

The PrintOut method has several optional parameters, and here we use the simplest format. This will print a copy of the default printer linked to our Windows machine.


We can print a preview by using PrintPreview. In a purely automated scenario, of course, we use printout to print directly.

Before exiting the Word application, we need to wait a little longer because the print job takes time to completely exit the background. If there is no delay (3), the $w->quit will be executed immediately, and the print job is immediately terminated.

Finally, we call $w->quit (false) to choose to close the Word application through our PHP script call. The only parameter provided here is to indicate whether we want to save the changes before exiting. We did make changes to the documentation, but we didn't want to save them because we wanted to provide a clean template for other users ' input.

After we have finished coding, we can load the form page, enter some content and submit the form. The following shows the output of the PHP script and updates the Word document:

Improved encoding speed and better understanding of PIA

PHP is a weak type of language. A COM object is an object type. In our PHP coding process, we were unable to use code Auto-hints and completion 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 it supports.


This will greatly reduce the speed of our development. To make development faster, first of all, I suggest that our development capabilities in C # should be migrated to our PHP code. I recommend a free C # IDE called "#develop" that you can download here. I like this software more than VS, because #develop is smaller, more concise, and responds faster.

C # code migration to PHP is not scary at all. Let me show some C # code first:
Copy the Code code 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 code for C # is exactly the same as the code base of the PHP we showed earlier. Because C # is a strongly typed language, we can see some types of conversion statements, and we have to explicitly assign a type to our variables.

With the type of code, we can enjoy the code automatically prompt and code completion function, so that our development speed will be greatly improved.


Another way to give us faster PHP development is to use Word's macro commands. Let's go through the actions we need to repeat, and then record them with a macro. A macro is actually visual Basic and can also be easily translated into PHP.

Most importantly, the Office PIA Microsoft Official documentation, especially the namespaces for each Office app in the documentation, is always the most desired reference for us. The more commonly used 3 applications are as follows:

    • 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 demonstrated how to use the PHP COM library and Microsoft Office Interop features to make 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 is also PHP, which is necessary for any programmer who develops PHP on the Windows platform.

With the COM extension of PHP, the door to mastering 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 am very much looking forward to more real-life application development that can be used this way.

http://www.bkjia.com/PHPjc/1026545.html www.bkjia.com true http://www.bkjia.com/PHPjc/1026545.html techarticle tutorial on using PHP to generate Word documents under Windows system first, make sure that a typical WAMP environment is installed and configured on your Windows system. Due to the Intero ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.