![]() |
|
sfTinyDocPlugin - 1.0.2symfony sfTinyDoc plugin |
|
The sfTinyDocPlugin allows to generate OpenOffice (OpenDocument) and Word 2007 documents with TinyButStrong template engine.
Prerequisite :
History :
The previous class was named tbsOOo. I rewrite the class to have a clean code with new methods and add some new functionnality like :
The office formats supported :
It's useful :
It's not :
Avantages :
moduleName/templates/actionNameSuccess.odtOnly for OpenDocument (OpenOffice) :
Limitations :
Can't merge collection of objects (iterator not supported in TinyButStrong, soon in next version), you have to transform to an array before.
asArray() in the Jobeet tutorial, see day 16.$result->toArray (false | true) to transform the result to an array, see : doctrine apiCan't change a style of the Office document by code, the styles are fix when you editing your Office document.
[var.xxx], use method mergeXmlField() in place of use globals varsheadergrp syntaxInstall sfTinyDocPlugin :
$ pear channel-discover pear.symfony-project.com
$ symfony plugin-install http://plugins.symfony-project.com/sfTinyDocPlugin
Or from the SVN repository
$ svn co http://svn.symfony-project.com/plugins/sfTinyDocPlugin/trunk/
Optionally add the following config to app.yml :
all:
sf_tiny_doc_plugin:
zip_method: shell # the method to zip/unzip : shell | ziparchive
zip_bin: zip # the binary to zip for 'shell' method
unzip_bin: unzip # the binary to unzip for 'shell' method
process_dir: %SF_WEB_DIR%/tmp # the process directoryClear your cache :
$ ./symfony cc
Configure the method to zip/unzip office documents :
Method 1 : shell (by default)
To install Zip on Red Hat Enterprise Linux or CentOS
$ yum install zip
Method 2 : ziparchive
To install Pecl ZipArchive on Red Hat Enterprise Linux or CentOS
$ yum install httpd-devel # if necessary
$ pecl install zip
Create a directory where to process temporary files, by example on web root :
$ mkdir web/tmp
$ chmod 777 web/tmp
Create a new module doc in your application
Create an action basic in doc/actions/actions.class.php
<?php public function executeBasic(sfWebRequest $request) { // create the document $doc = new sfTinyDoc(); $doc->createFrom(); $doc->loadXml('content.xml'); $doc->mergeXmlField('field1', 'variable'); $doc->mergeXmlField('field2', array('id' => 55, 'name' => 'bob')); $doc->mergeXmlField('field3', $doc); $doc->mergeXmlBlock('block1', array( array('firstname' => 'John' , 'lastname' => 'Doe'), array('firstname' => 'Douglas', 'lastname' => 'Adams'), array('firstname' => 'Roger' , 'lastname' => 'Waters'), ) ); $doc->saveXml(); $doc->close(); // send and remove the document $doc->sendResponse(); $doc->remove(); throw new sfStopException; }
Create an OpenOffice text document (.odt) and paste this.
$doc->mergeXmlField() with a string
[field1]
$doc->mergeXmlField() with an array
[field2.id] [field2.name]
$doc->mergeXmlField() with an object
[field3.getZipMethod]
[field3.getZipBinary]
[field3.getUnzipBinary]
$doc->mergeXmlBlock() with an array
[block1;block=begin][block1.$] [block1.firstname] [block1.lastname]
[block1;block=end]
Num rows : [block1.#]
Save the document as basicSuccess.odt in template directory doc/templates/
You can now test my first doc in a browser the result : doc/basic
The result is something like this :
$doc->mergeXmlField() with a string
variable
$doc->mergeXmlField() with an array
55 bob
$doc->mergeXmlField() with an object
shell
zip
unzip
$doc->mergeXmlBlock() with an array
0 John Doe
1 Douglas Adams
2 Roger Waters
Num rows : 3
Change in your action
<?php $doc->createFrom();
By
<?php $doc->createFrom(array('extension' => 'ods'));
Create an OpenOffice spreadsheed document (.ods) and paste the same as before.
$doc->mergeXmlField() with a string
[field1]
$doc->mergeXmlField() with an array
[field2.id] [field2.name]
$doc->mergeXmlField() with an object
[field3.getZipMethod]
[field3.getZipBinary]
[field3.getUnzipBinary]
$doc->mergeXmlBlock() with an array
[block1;block=begin][block1.$] [block1.firstname] [block1.lastname]
[block1;block=end]
Num rows : [block1.#]
Save the document as basicSuccess.ods in template directory doc/templates/
You can now test my OpenOffice calc document in a browser the result : doc/basicCalc
Change in your action
<?php $doc->createFrom(); $doc->loadXml('content.xml');
By
<?php $doc->createFrom(array('extension' => 'docx')); $doc->loadXml('word/document.xml');
Create an Word2007 document (.docx) and paste the same as before.
$doc->mergeXmlField() with a string
[field1]
$doc->mergeXmlField() with an array
[field2.id] [field2.name]
$doc->mergeXmlField() with an object
[field3.getZipMethod]
[field3.getZipBinary]
[field3.getUnzipBinary]
$doc->mergeXmlBlock() with an array
[block1;block=begin][block1.$] [block1.firstname] [block1.lastname]
[block1;block=end]
Num rows : [block1.#]
Save the document as basicSuccess.docx in template directory doc/templates/
You can now test the Word2007 document in a browser the result : doc/basicWord
<?php $doc->createFrom(); // the defaut extension is 'odt' $doc->loadXml(); // the defaut XML filename is 'content.xml'
The documents are a zip archive format with these main files :
OpenDocument Word2007
content content.xml word/document.xml
meta meta.xml ?
settings settings.xml word/settings.xml
styles styles.xml word/styles.xml
header styles.xml word/header1.xml
footer styles.xml word/footer1.xml
content.xml is the main file to merge in OpenOffice documents.
word/document.xml is the main file to merge in Word 2007 documents.
content.xml is the defaut file in loadXml() method.
See more on :
The XML tags you have to know :
HTML tags OpenDocument XML tags Word2007 XML tags
table <table> <table:table> <w:tbl>
row <tr> <table:table-row> <w:tr>
cell <td> <table:table-cell> <w:tc>
paragraph <p> <text:p> <w:p>
loadXml() method.After merging data, you have to save the result of merging in the XML file with the saveXml() method.
Each time you have to merge data with an another file you have to
<?php $doc->loadXml('content.xml'); // ... $doc->mergeXml(...); $doc->mergeXmlField(...); $doc->mergeXmlBlock(...); $doc->mergeXml(...); // ... $doc->saveXml(); $doc->loadXml('styles.xml'); // ... $doc->mergeXml(...); $doc->mergeXmlField(...); $doc->mergeXmlBlock(...); $doc->mergeXml(...); // ... $doc->saveXml();
There two different methods to merge data, field and block
method : mergeXmlField($name, $data)
MergeField it's for fields (one time)
method : mergeXmlBlock($name, $data)
MergeBlock it's for repetive fields to merge all items from an array.
TIPS
If you don't define the begin block and the end of the block, the fields are merged one time.
The two methods call the generic method : mergeXml($options, $data)
With this method, you can pass some parameters in the $options array like :
Example :
[php]
<?php
$doc->mergeXml(
array(
'name' => 'b1',
'type' => 'block',
'data_type' => 'array',
'charset' => 'UTF-8',
'is_escape' => true,
),
$data
);
For all parameters of TinyButStrong, have a look to the TinyButStrong manual
image : This parameter is to merge image automaticaly
image=max the merged image is limited to the maximum template's image size
Examples :
[field.src;image] [block.src;image] [block.src;image=50%] [block.src;image=max] [block.src;image=fit]
NOTES
Note #1 : The TBS tag is set in the image name. Use OpenOffice dialog box image property to edit.
Note #2 : The file image is added into the document and you don't have to use the
addFile()method.Note #3 : If the image is empty or not found, the image is removed.
link : This parameter to transform to a linkable text
type : This parameter fixes the type of the cell, not the format (only for spreadsheet).
type=time or type=t. Input data is string like 'H:i:s'.
Examples :
[field;type=n] [field.data;type=date] [block.data;type=percentage] [block.data;type=time]
NOTES
Note #1 : Only one TBS field can be merge in a cell when the parameter
typeis set.Note #2 : To fix the format the cell, use OpenOffice dialog box format for that. Don't use
frmparameter.
As in French we can said, "Un dessin vaut mieux qu'un long discours", here some examples for sfTinyDocPlugin
Release 1.0.2
tinyDoc::VERSIONRelease 1.0.1
Release 1.0.0
add formula support in spreadsheet
<?php <table:table-cell table:formula="of:=IF([.B2]>0;"true";"false")" office:value-type="string" office:string-value="true"> <text:p>true</text:p> </table:table-cell>
add optionals parameters in method sendResponse()
support native format (float, percentage, currency, date, time) in word processing tables (* not easy)
support merge images with a block in spreadsheet (* not easy)
next TinyButStrong version (3.5 ?) to support iterators and a new parameter to merge sub-block.
Mailing-list : http://groups.google.fr/group/tinydoc
Please email to the mailing-list tinydoc@googlegroups.com for support (register only).
Bug tracker : http://trac.symfony-project.org/browser/plugins/sfTinyDocPlugin
The tbsOOo class is no longer supported, and I strongly discourage his use.
If you have feature suggestions, bug reports, patches, usage examples for the documentation or want to become an active contributor, send me an email to: olivierloynet [at] gmail [dot] com
Any help is welcome!
Special thanks to Vincent who write TinyButStrong.
To Romain, Pierre and Cecile.