COM functions in PHP4 (windows)

Source: Internet
Author: User
Tags adobe distiller
In PHP4 (windows), the COM function in the COM function PHP4 (windows) has been writing an excel file into mysql over the past few days. I found an article and searched for the phpx forum. I didn't have this post, post it as follows:


COM functions in PHP4 (windows)


Introduction

COM functions built in PHP4 are quite attractive for developing programs in win32 environment, but there are still few related technical documents. This article will take three examples

Don't deal with MS office 2000 Word, Excel, Adobe Distiller to explain how to use COM functions in PHP.

COM Technology was proposed and developed by Microsoft a few years ago. the terms mentioned in this article include OLE, OLE Automation, ActiveX, and COM.

Sample, which indicates that a piece of encapsulated code (object) is used to complete some functions of a windows application. The PHP4 COM function can connect to an object instance and use the method

Attribute.

If you want to use the source code of the following example, please refer to my configuration.

Windows 98-MS Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI


COM mark in PHP4

Now let's get started. to instantiate a component with COM of PHP4, we need the "OLE program identifier" of the new operator and object ":


$ Instance = new COM ("$ identifier ");

?>

Because COM is a reserved word of PHP4, it transmits the identifier of this object to a constructor. now we get an example of this component. based on the nature of the OOP class, we can easily

To access its methods and attributes.

For example:


$ Instance-> [Object]-> [method1]-> [method2]->...-> [property];

?>

That's easy!

The structure of OOP cannot work in PHP (due to PHP syntax problems, the attribute name. the value is invalid characters, such as vertices and parentheses. Therefore, PHP4 provides two functions:


Bool com_set (class com_object, string property name, string property_value );

Mixed com_get (class com_object, string property_name );

?>

Finally, PHP4 also supports DCOM technology. you can create an object instance on a remote computer.


$ Instance = new COM (string "Component name", string "remote_server_address ");

?>

Note: this is to use the DCOM command to set PHP. In the future, PHP developers will provide DCOM support for Unix.

Identifiers, methods, and attributes

The identifier is a string as follows:

MS Word: "Word. Application" or "Word. Application.9"
MS Excel: "Excel. Application" or "Excel. Sheet"
ADOBE Acrobat: "Exch. application" or "sharedistiller. sharedistiller"

For the last identifier, it is not easy to obtain the correct object identification name. If you cannot access the VBA document, you can find the windows registration

Table, search for it in HKEY_CLASSES_ROOT to get the names of some applications. Put the valid object ID on your machine in the CLSID subfolder.

Generally, an application provides a document to describe its COM method and attributes. In office2000, you can run a program, open the VBA editor, and select the object editor. Input application

Library, right-click a class or member name in the window below, and click help to get a description of this class or member. You too

Refer to MSDN. An Excel example is as follows: [url] http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm#/url]


Operate MS Word with COM function

Now let's start with the first example:


#*************************************** ******************
# This example is from the Zend site and slightly changed
# Open a word instance and create a new document Useless test.doc
# Enter a line of text "This is a test2 ..."
#*************************************** ******************

# Instantiate an object

$ Word = new COM ("word. application") or die ("Unable to instantiate Word ");

# Obtain and display the version

Print "Loaded Word, version {$ word-> Version}
";

# Another method to get the version

$ Testversion = com_get ($ word-> application, version );

Print "Version using Com_get (): $ testversion
";

# Make it visible

$ Word-> Visible = 1;

# Creating a new file

$ Word-> Documents-> Add ();

# Write characters

$ Word-> Selection-> TypeText ("This is a test ...");

# Save

$ Word-> Documents [1]-> SaveAs ("Useless test.doc ");

# Disable

$ Word-> Quit ();

?>

You only need to spend a few minutes to read this program, and refer to the Word OLE technical documentation, you will learn almost all the operations you need in your own program.

MS Excel in the use of php com functions

Like the Word example above, you should learn this example and refer to the help document of the object browser in the Visual Basic Editor of Excel.


# Open the workbook and its sheet,
# In this example, a workbook is the SOLVSAMP. XLS

$ Workbook = "C: Program FilesMicrosoft officeOfficeSamplesSOLVSAMP. XLS ";
$ Sheet = "Quick Tour ";

# Instantiate a Component Object
$ Ex = new COM ("Excel. sheet") or Die ("Did not connect ");

# Obtain the program name and version
Print "Application name: {$ ex-> Application-> value}
";
Print "Loaded version: {$ ex-> Application-> version}
";

# Open the work book so that we can use it
$ Wkb = $ ex-> application-> Workbooks-> Open ($ workbook) or Die ("Did not open ");

# Pre-save the original work book and create a copy of the work book
$ Ex-> Application-> ActiveWorkbook-> SaveAs ("Ourtest ");
# $ Ex-> Application-> Visible = 1; # annotate this sentence to make it Visible to Excel

# Read and write a cell in a new worksheet
# We can read this cell E11 (Advertising in the 4th. Quarter)
$ Sheets = $ wkb-> Worksheets ($ sheet); # Select the sheet
$ Sheets-> activate; # Activate it
$ Cell = $ sheets-> Cells (11, 5); # Select the cell (Row Column number)
$ Cell-> activate; # Activate the cell
Print "Old Value = {$ cell-> value}
"; # Print the value of the cell: 10000
$ Cell-> value = 15000; # Change it to 15000
Print "New value = {$ cell-> value}
"; # Print the new value = 15000

# Finally, re-calculate the cell with the new value
$ Sheets-> Calculate;
# REQUIRED. manual calculation is optional.
# Total effect value (cell E13)
$ Cell = $ sheets-> Cells (13, 5); # Select the cell (Row Column number)
$ Number = Number_format ($ cell-> value );
Print "New Total cost = $ number-was $47,732 before.
";
# According to the calculation formula, the advertisement affects the company's overhead. here, $57,809 is displayed.

# Use Excel built-in functions
# PMT (percent/12 months, Number of payments, Loan amount)
$ Pay = $ ex-> application-> pmt (0.08/10000 );
$ Pay = sprintf ("%. 2f", $ pay );
Print "Monthly payment for $10,000 loan @ 8% interest/10 months: $ pay
";

# Shocould print monthly payment = $-1,037.03

# (Optional) save
$ Ex-> Application-> ActiveWorkbook-> SaveAs ("Ourtest ");
# Close, no questions
$ Ex-> application-> ActiveWorkbook-> Close ("False ");
Unset ($ ex );

?>

This example allows your PHP to work with Excel. of course, more objects can be used to access a self-written OOP encapsulation class, which is as easy as accessing excel.

Access Adobe Distiller using PHP COM

The last example is not a MS program. if your program has a PostScript file, you will be interested in it. rewrite (extract) it into a PDF file. Adobe has

Programs are called Distiller, which can generate an instance. The code is as follows:


$ Pdf = new COM ("nvidistiller=distiller.1 ");

?>

Note that the OLE identification name "nvidistiller" provided in the Distiller document is invalid.

The most basic method to extract a file is:


$ Pdf-> FileToPdf ($ psfile, strOutputPDF '', strJobOptions "");

?>

This $ psfile is the PostScript file name, and strOutputPDF is the file name of the output file PDF. StrJobOptions is the Distiller parameter file name. The last two parameters

It is optional and the same name is used by default. The PS file name and PDF file name use the default Joboptions file. For example:


$ Pdf-> FileToPdf ($ psfile ,"","");
# Here $ psfile can be Myfile. ps. the myfileworkflow file will be returned.

?>

More methods and attributes can be used in Distiller. If you are interested, please refer to Adobe's technical documentation.


Abort/possible problems

If an error occurs in your code, you may create an instance, but it is not properly disabled. Worst of all, this application may be maintained by this instance.

If there are multiple copies of the program in your program list, even if you correct this error, it will interfere with your results. Solution: clear a bug as soon as it is fixed.

Use and end the task before you start again. For the same reason, close the program and delete the instance at the end of your code.

You have some tips when dealing with com_get and com_set exceptions. For example:
$ Version = Com_get ($ instance-> Application, "Version ");

It will work in Word but an error will be generated in Excel.

Some objects cannot be instantiated in PHP4 because this program requires a custom interface, but PHP4 does not.


Why should we use it?

I hope these three examples can give you some clues. PHP COM allows you to access windows4 programs in PHP scripts. This code is simpler than ASP and can be integrated with other

PHP provides powerful database support functions. Microsoft has made great efforts to sell this COM technology in various aspects, with different names and structures, such as COM + (Combine COM

Microsoft Transaction Server MTS), ADO, ole db, OWC, Windows DNA, and so on. The combination of PHP and Apache provides an open source code solution.

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.