PHP Articles
Basic & Beginners
Advanced
Database
XML, Webservices
Design Patterns
Ajax
All Articles
Main Menu
Home
About PHP Hacks
Links
Contact Us
Search


Generate OpenOffice documents with PHP PDF Print E-mail
Monday, 11 September 2006
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.

Comments
Add NewSearchRSS
Marie Osmond - This is my test IP:64.5.147.100 | 2006-09-25 13:37:23
You should see what this is outputting!!!!!!!
François Girault - Broken documents IP:194.250.151.164 | 2006-10-12 09:06:54
When opening in OO (2.0), sxw are not well recognized. OO shows import filter. I select "OO document", this OO tells file is broken, wants to repair it, and sometime repair fails ..
mec - potentials IP:125.204.107.172 | 2006-10-20 23:52:49
This could be such a good thing for many organizations. Right now, we output pdf for our users from the DB, but office workers cannot modify or use the content in other documents. Neither can they change the formatting. This project could really help us and move our users to OpenOffice at the same time.

However, I got the following error message when I tried to use PHP DocWriter on my private hosting service, which is running in SafeMode (that may be the problem):

Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of array_push(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. in /phpdocwriter/lib/phpdocwriter/pdw_document.php on line 381

So I haven't seen it in action yet, but I have high hopes.
Anonymous IP:82.70.68.43 | 2006-11-30 12:11:13
very good but dificult
Nebe - Same Error IP:196.33.246.18 | 2007-01-09 13:54:29
I get the same error as mec...

Any idea what could be wrong?
Dimitri Manchuelle - RE: same error IP:84.96.10.190 | 2007-01-10 19:09:50
The message you have isn't an error, but a warning. So you can continue to use the package without worrying about this warning. To shut up it, just do what the message says :
"you can set allow_call_time_pass_reference to true in your INI file"

(another solution would be to modify the source code of phpWriter in pdw_document.php on line 381
to pass the incriminated parameter by reference adding a "&" before it)
Didier IP:81.242.238.91 | 2007-01-19 08:39:02
It doesn't works very well with Oo 2.0.

No solution found. I must use Oo 1
Mohan - Method for invoking soffice th IP:164.100.215.81 | 2007-02-21 11:42:23
Any idea how soffice could be invoked through PHP to open a file.
Pankaj - how to use phpdocriter for php IP:125.19.36.82 | 2007-04-06 12:25:59
I have checked this library in php4 and it works fine. But i have one problem that is i am using php5. Is it possible to use this library in php5? I would appreciate if some one can suggest any solution for php5.

Thanks in advance
isaac - rtf to pdf IP:212.79.138.120 | 2007-04-26 13:34:03
if i have a rtf file, can i covert it to pdf using these libraries?
hjezmmzf - hjezmmzf IP:201.17.116.184 | 2007-07-10 15:33:10
awqzwfxc http://kehplljq.com naczihmt kmbxevku [URL=http://imqthymc.com]jfvfonrp[/URL] hjzaxoji
Only registered users can write comments!
Last Updated ( Monday, 11 September 2006 )
 
< Prev   Next >

Syndicate


Login Form





Lost Password?
No account yet? Register