Instructions for downloading, compiling and installing are available at the Installation page.
The auxiliary function get_remote_xcomponent must be called before any other API or auxiliary method. This function initializes the "connection" to the Office and allocates any internal data required by the module.
It takes two parameters. The first, is the UNO URL and the second is the name of the "first service" to be created.
As a general guide, using the call bellow should be sufficient to get a valid connection to OpenOffice.
$var=get_remote_xcomponent(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager",
"com.sun.star.frame.Desktop");
Puno uses the UNO Reflection API to access objects and execute methods from the OpenOffice.org API. It is important to note that PHP functions and methods names are case-insentive, but those accessed through the Reflection API are not. This means that you must always write objects names exactly as they are documented in the Developers Guide. This rule is valid for objects attributes and methods names as well.
API defined structs are also created through the Reflection API. For this purpose, the special function create_struct is available. It takes just one parameter, the name of the struct to be created.
Differently from Java and C++ bindings, in PHP it is not necessary to query interfaces from objects or services. This is automatically done by the Reflection API. So you just need to call the desired method from the object and the correct interface will be used.
IMPORTANT: As saw in the examples, the method dispose() must be called at the end of every script, otherwise the office running on the server may crash or become unstable.
Follow the steps described in the OpenOffice Developers Guide to configure your office installation to listen on a TCP socket.
Or, the simplest way, just start the Office with the following line:
<OFFICEPATH>/program/soffice "-accept=socket,port=8100;urp;"
You can use the command "netstat -an | grep 8100" to identify whether the Office is listening on the TCP port or not.
OpenOffice.org must be configured with Java support enabled in order to receive TCP/IP connections.
As said earlier, the connection to OpenOffice.org is made through an auxiliary function:
get_remote_xcomponent
It takes two parameters:
get_remote_xcomponent must be called before any other OpenOffice method or auxiliary function. This is necessary so it can initialize internal components used to create structs and other services instances.
Using the call as it's bellow is sufficient to get a valid connection to OpenOffice:
$var=get_remote_xcomponent(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager",
"com.sun.star.frame.Desktop");
Structs are also created through the Reflection API.
For this purpose, the special function create_struct is available.
It takes just one parameter, the full name of the struct to be created.
For example, if you need to create an instance of the struct TableBorder you must enter:
create_struct("com.sun.star.table.TableBorder");
Simple types occur in structs, method return values or parameters. The following table shows the simple types in UNO and, if available, their exact mappings to Java, C++ and PHP.
UNO |
Type description |
Java |
C++ |
PHP |
---|---|---|---|---|
char |
16-bit unicode character type |
char |
sal_Unicode |
String¹ |
boolean |
boolean type; true and false |
boolean |
sal_Bool |
Boolean |
byte |
8-bit ordinal type |
byte |
sal_Int8 |
Integer |
short |
signed 16-bit ordinal type |
short |
sal_Int16 |
Integer |
unsigned short |
unsigned 16-bit ordinal type |
- |
sal_uInt16 |
Integer |
long |
signed 32-bit ordinal type |
int |
sal_Int32 |
Integer |
unsigned long |
unsigned 32-bit type |
- |
sal_uInt32 |
Float² |
hyper |
signed 64-bit ordinal type |
long |
Float² |
Float² |
unsigned hyper |
unsigned 64-bit ordinal type |
Float² |
sal_uInt64 |
Float² |
float |
processor dependent float |
float |
float (IEEE float) |
Float³ |
double |
processor dependent double |
double |
double (IEEE double) |
Float³ |
Full API documentation is available online at
http://api.openoffice.org/DevelopersGuide/DevelopersGuide.html
Almost all examples are written in and "for" Java, so you need to consider the General Programming Guidelines when translating Java examples to PHP.
On windows you can access the OpenOffice API using PHP's COM module.
If you wish to create fully compatible code between Windows and Linux/Unix, then you need to write the userspace auxiliary functions implemented by Puno. Also, constants like FontWeight_BOLD have to be defined before you can use them.
The file puno.inc contains all the code necessary to make your scripts portable between Linux/Unix (Puno) and Windows (COM). You just need to include it at the beginning of your files (Windows only).
In the code, the two auxiliary functions create_struct and get_remote_xcomponent are implemented as follows:
<?php
Take a look at http://www.oooforum.org/forum/viewtopic.phtml?t=3474 for more information about PHP5/COM integration.
$GLOBALS[ '__puno_srv_manager' ] = NULL
;
//get connection
function get_remote_xcomponent( $uno_url, $first_service )
{
$srv_manager = new COM("com.sun.star.ServiceManager");
$GLOBALS[ '__puno_srv_manager' ] = $srv_manager;
$core_reflection = $srv_manager->createInstance("com.sun.star.reflection.CoreReflection");
$GLOBALS[ 'core_reflection' ] = $core_reflection;
$srv = $srv_manager->createInstance( $first_service );
return $srv;
}
//create structs
function create_struct( $name )
{
$srv_manager = $GLOBALS[ '__puno_srv_manager'];
$new_struct = $srv_manager->Bridge_GetStruct( $name );
return $new_struct;
}
//
?>
IMPORTANT: PHP's COM implementation seems to have a problem when dealing with Empty Arrays. For now, the only possible solution is to create an array with at least one element and fill it with an acceptable default value.
<?php
$load_url="private:factory/".$doc_type;
$property = create_struct( "com.sun.star.beans.PropertyValue" );
$property->Name = "ReadOnly";
$property->Value = FALSE;
$load_props = array();
$load_props[0] = $property;
$x_component_loader->loadComponentFromURL($load_url,"_blank",0, $load_props);
?>
<?php
$load_url="private:factory/".$doc_type;
$load_props = array();
$x_component_loader->loadComponentFromURL($load_url,"_blank",0, $load_props);
?>