PHP DocWriter is a set of PHP classes that allows you to create documents in OpenOffice.org XML format (that is, StarOffice/OpenOffice.org documents). You can download the library files from here (http://phpdocwriter.sourceforge.net/download.php?lang=en). Also, you can find installation steps in the mentioned link.
It is also capable of communicating with the OpenOffice.org application with the purpose of converting OpenOffice.org XML generated documents to any other format supported by the OpenOffice.org application in an automatic and transparent way.
Functionality
The class is still under development but it's fully functional right now. At the moment it has the following features:
1. Page headers and footers You can define various classes of pages on your documents (first page, index, standard, etc.) and assign a different header/footer for each class.
2. Textboxes
3. Images
4. Tables
5. Drawings
6. Styles management
- Page styles
- Paragraph styles
- Character styles
7. Meta-information of the document Meta information of the document, includes the document information such as the author or the title of the document.
8. Automatic conversion of created documents to other formats This is achieved using the UNO interface of OpenOffice.org application. The library communicates with the application through this interface in order to convert documents to other formats. The library cannot convert documents itself, it needs OpenOffice.org to be installed and correctly configured to accept conversions. Currently the formats that are supported are MS Word, PDF, RTF, StarWriter, LaTeX, XHTML, HTML, etc.
9. Misc Some miscellaneous features of the library:
- Links
- Bookmarks
- Page breaks
- Predefined fields
Fields like page number or number of pages of the document.
Examples
Here are some examples that create simple documents. These examples work with the latest version of the library (currently 0.3 version).
Simple example: insert text
Lets start off with a simple ‘Hello World’.
<?php
require_once('../lib/include.php');
import('phpdocwriter.pdw_document');
$sxw = new pdw_document;
$sxw->SetLanguage('en', 'US');
$sxw->SetStdFont('Times New Roman', 10);
$sxw->Write('Hello World!');
$sxw->Output();
?> Text styles and line breaks
An example on using various text styles and line breaks.
<?php require_once('../lib/include.php');
import('phpdocwriter.pdw_document');
$sxw = new pdw_document;
$sxw->SetLanguage('en', 'US');
$sxw->SetStdFont('Times New Roman', 10);
$sxw->SetFont(array('family'=>'Verdana', 'style'=>'BI', 'size'=>27));
$sxw->Write('John Doe');
$sxw->SetFont(array('family'=>'Arial', 'style'=>'U', 'size'=>22));
$sxw->Write('Mark');
$sxw->Ln();
$sxw->SetFont (array('family'=>'Comic Sans MS', 'size'=>32));
$sxw->Write ('Edarta');
$sxw->Ln();
$sxw->SetFont (array('family'=>'Georgia', 'style'=>'B', 'size'=>22));
$sxw->Write ('Nick ');
$sxw->Output();
?> Textboxes and Images
An example to show the usage of Text Box and Image
<?php
require_once('../lib/include.php');
import('phpdocwriter.pdw_document');
import("phpdocwriter.pdw_textbox");
$sxw = new pdw_document;
$sxw->SetLanguage('en', 'US');
$sxw->SetStdFont ('Times New Roman', 10);
$sxw->Write('Lets draw some boxes...');
$sxw->Ln(2);
$tb1 = new pdw_textbox ($sxw, array ('w'=>15, 'h'=>2, 'x'=>1, 'y'=>0.5));
$tb1->Write('Text Box 1');
$tb1->Insert();
$tb2 = new pdw_textbox ($sxw, array ('w'=>5.5, 'h'=>5, 'x'=>1, 'y'=>3));
$tb2->Write('An image inside the text box...');
$tb2->Ln();
//You can use any .png image with width = 4.12 and height = 1.92
$tb2->Image(array('path'=>'logo.png', 'w'=>4.12, 'h'=>1.92));
$tb2->Insert();
$tb3 = new pdw_textbox ($sxw,array ('w'=>9, 'h'=>4, 'x'=>7, 'y'=>3));
$tb3->Write('Inserts links inside: ');
$tb3->AddLink("phphacks", "http://www.phphacks.com");
$tb3->Insert();
$sxw->Output();
?> Header, footer and meta-information
<?php
require_once('../lib/include.php');
import('phpdocwriter.pdw_document');
import('phpdocwriter.pdw_header');
import('phpdocwriter.pdw_footer');
$sxw = new pdw_document;
$sxw->SetLanguage('en','US');
$sxw->SetStdFont ("Times New Roman",10);
$sxw->AddPageDef(array('name'=>'Standard','margins'=>'2,2,2,2'));
$head = new pdw_header($sxw,'Standard');
$head->Ln();
$head->Image(array('path'=>'logo.png','w'=>4.12,'h'=>1.92));
$head->Insert();
$foot = new pdw_footer($sxw,'Standard');
$foot->Ln();
$foot->AddLink ("phphacks"," http://www.phphacks.com");
$foot->Insert();
$sxw->SetTitle ('Document PHPHACKS');
$sxw->SetAuthor ('Joeri');
$sxw->SetSubject ('Sample document created using phpdocwriter');
$sxw->SetKeyWords ('php,OpenOffice.org,phpdocwriter');
$sxw->Write ('This is a sample document!!!');
$sxw->Output();
?> Tables
<?php
require_once('../lib/include.php');
import('phpdocwriter.pdw_document');
$sxw = new pdw_document;
$sxw->SetLanguage('en','US');
$sxw->SetStdFont ("Times New Roman",10);
$sxw->Write ('Sample Table:');
$sxw->Ln(2);
$header = array('ID','First Name','Last Name','Tel','Email');
$data[] = array('23453451F','John','Doe','912345678','john at dom.com');
$data[] = array('36213764H','Cowboy','Beebob','915436753','beebob at dom.com');
$sxw->Table($header,$data);
$sxw->Output();
?> Summary
That should get you started on generating OpenOffice.org documents using PHP. Using the power of PHP DocWriter you can create OpenOffice.org documents without even having the application (OpenOffice.org) installed in your server.
|