COM functions in PhP4 (Windows)

Source: Internet
Author: User
Tags adobe distiller

COM functions in PhP4 (Windows)

I have been writing an Excel file into MySQL over the past few days.Article, Search for the phpx Forum. If you do not have this post, post it as follows:

COM functions in PhP4 (Windows)

Introduction

The com function built in PhP4 is developed in Win32 environment.ProgramIt is quite attractive, but there are still not many 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, all of which are encapsulated in a segmentCode(Object) 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.