Development

/plugins/sfTinyDocPlugin/trunk/README

You must first sign up to be able to contribute.

root/plugins/sfTinyDocPlugin/trunk/README

Revision 17899, 17.0 kB (checked in by oloynet, 4 years ago)

add new constants for parameter 'image'

Line 
1 # sfTinyDocPlugin
2
3
4 ## Overview
5
6 The sfTinyDocPlugin allows to generate __OpenOffice__ (OpenDocument) and __Word 2007__ documents with TinyButStrong template engine.
7
8   * __Prerequisite :__
9
10     * PHP 5.2
11     * symfony framework 1.0, 1.1 and 1.2 (for the plugin)
12
13   * __History :__
14
15   The previous class was named [tbsOOo](http://www.tinybutstrong.com/tbsooo.php). I rewrite the class to have a clean code with new methods and add some new functionnality like :
16     * works with the last version of [TinyButStrong](http://www.tinybutstrong.com)
17     * better support of specials characters like carriage return, new line and tab
18     * can generate __Word 2007__ documents
19     * can add __images__ (only OpenDocument)
20     * can merge data with __native format__ in OpenOffice spreadsheet
21     * two methods to zip and unzip the office files (zip with command line and the pecl ZipArchive)
22     * and a plugin for __[symfony framework](http://www.symfony-project.org/plugins/sfTinyDocPlugin)__ : sfTinyDocPlugin
23
24   * __The office formats supported :__
25
26     * OpenOffice 1.0 or OpenDocument 1.0
27     * OpenOffice 2.0 or OpenDocument 1.1
28     * Word 2007
29
30   * __It's useful :__
31
32     * To create word processing and spreadsheet documents.
33     * To create reports, bill, orders.
34     * To create mailing.
35     * To create multi pages documents.
36     * To use the power of the TinyButStrong template engine syntax :
37       * Merge fields
38       * Merge blocks (repetitive fields)
39       * and more...
40
41   * __It's not :__
42
43     * To create an Office document from scratch.
44     * To convert a document from OpenOffice to Word or to PDF or else. See the project [PyODConverter](http://www.artofsolving.com/opensource/pyodconverter) to do that.
45
46   * __Avantages :__
47
48     * __No installation of OpenOffice__ on the server side.
49     * Works on *nix, win and other systems.
50     * Templates can be created and modified __easyly__ by designer.
51     * Templates layouts and styles are made with __OpenOffice__ or __Word2007__ application.
52     * Data in spreadsheet are converted to native OpenOffice format with the __new parameter__ named 'type'.
53       * Useful to use with spreadsheet formulas
54       * Useful to use the spreadsheet format
55     * __For the [symfony framework](http://www.symfony-project.org)__
56       * Works with sf1.0, sf1.1 and sf1.2
57       * No dependance with ORM
58       * Template have the same location as the symfony templates.
59       * Template name have the same name as the symfony templates. __`moduleName/templates/actionNameSuccess.odt`__
60       * Get the config from app.yml.
61
62   * __Limitations :__
63
64     * Can't merge collection of objects (iterator not supported in TinyButStrong, soon in next version), you have to transform to an array before.
65       * For __Propel__ write your own method like __`asArray()`__  in the Jobeet tutorial, [see day 16](http://www.symfony-project.org/jobeet/1_2/Propel/en/16).
66       * For __Doctrine__ use the magic method : __`$result->toArray (false | true)`__ to transform the result to an array, see : [doctrine api](http://www.doctrine-project.org/documentation/manual/1_0/en/working-with-models:arrays-and-objects:to-array)
67
68     * Can't change a style of the Office document by code, the styles are fix when you editing your Office document.
69     * Can't merge global vars __`[var.xxx]`__, use method __`mergeXmlField()`__ in place of use globals vars
70     * Can't merge sub-block because TinyButStrong need global vars, see __`headergrp`__ syntax
71
72   * __Experimental for OpenOffice :__
73
74     * Add images dynamicaly, to make a catalog by example - The image size has to be the same as in the layout.
75
76
77 ## Installation
78
79   * Install sfTinyDocPlugin :
80
81         $ pear channel-discover pear.symfony-project.com
82         $ symfony plugin-install http://plugins.symfony-project.com/sfTinyDocPlugin
83
84     Or from the SVN repository
85
86         $ svn co http://svn.symfony-project.com/plugins/sfTinyDocPlugin/trunk/
87
88   * Optionally add the following config to `app.yml` :
89
90         all:
91           sf_tiny_doc_plugin:
92             zip_method:    shell             # the method to zip/unzip : shell | ziparchive
93             zip_bin:       zip               # the binary to zip   for 'shell' method
94             unzip_bin:     unzip             # the binary to unzip for 'shell' method
95             process_dir:   %SF_WEB_DIR%/tmp  # the process directory
96
97   * Clear your cache :
98
99         $ ./symfony cc
100
101   * Configure the method to zip/unzip office documents :
102
103     * Method 1 : __shell__ (by default)
104
105       To install Zip on Red Hat Enterprise Linux or CentOS
106
107               $ yum install zip
108
109     * Method 2 : __ziparchive__
110
111        To install Pecl ZipArchive on Red Hat Enterprise Linux or CentOS
112
113               $ yum install httpd-devel
114               $ pecl install zip
115
116   * Create a directory where to process temporary files, by example on web root :
117
118         $ mkdir web/tmp
119         $ chmod 777 web/tmp
120
121
122 ## My first OpenOffice text document in action (.odt)
123
124   * Create a new module `doc` in your application
125
126   * Create an action `basic` in `doc/actions/actions.class.php`
127
128         [php]
129         <?php
130         public function executeBasic(sfWebRequest $request)
131         {
132           // create the document
133           $doc = new sfTinyDoc();
134           $doc->createFrom();
135           $doc->loadXml('content.xml');
136           $doc->mergeXmlField('field1', 'variable');
137           $doc->mergeXmlField('field2', array('id' => 55, 'name' => 'bob'));
138           $doc->mergeXmlField('field3', $doc);
139           $doc->mergeXmlBlock('block1',
140             array(
141               array('firstname' => 'John'   , 'lastname' => 'Doe'),
142               array('firstname' => 'Douglas', 'lastname' => 'Adams'),
143               array('firstname' => 'Roger'  , 'lastname' => 'Waters'),
144             )
145           );
146           $doc->saveXml();
147           $doc->close();
148
149           // send and remove the document
150           $doc->sendResponse();
151           $doc->remove();
152
153           throw new sfStopException;
154         }
155
156   * Create an OpenOffice text document (.odt) and paste this.
157
158         $doc->mergeXmlField() with a string
159
160         [field1]
161
162         $doc->mergeXmlField() with an array
163
164         [field2.id] [field2.name]
165
166         $doc->mergeXmlField() with an object
167
168         [field3.getZipMethod]
169         [field3.getZipBinary]
170         [field3.getUnzipBinary]
171
172         $doc->mergeXmlBlock() with an array
173
174         [block1;block=begin][block1.$] [block1.firstname] [block1.lastname]
175         [block1;block=end]
176         Num rows : [block1.#]
177
178
179   * Save the document as `basicSuccess.odt` in template directory `doc/templates/`
180
181   * You can now test my first doc in a browser the result : [doc/basic](http://tinydoc.unesolution.fr/doc/basic)
182
183   The result is something like this :
184
185         $doc->mergeXmlField() with a string
186
187         variable
188
189         $doc->mergeXmlField() with an array
190
191         55 bob
192
193         $doc->mergeXmlField() with an object
194
195         shell
196         zip
197         unzip
198
199
200         $doc->mergeXmlBlock() with an array
201
202         0 John Doe
203         1 Douglas Adams
204         2 Roger Waters
205
206         Num rows : 3
207
208
209 ## and for OpenOffice calc document (.ods)
210
211   * Change in your action
212
213         [php]
214         <?php
215         $doc->createFrom();
216
217   By
218
219         [php]
220         <?php
221         $doc->createFrom(array('extension' => 'ods'));
222
223
224   * Create an OpenOffice spreadsheed document (.ods) and paste the same as before.
225
226         $doc->mergeXmlField() with a string
227
228         [field1]
229
230         $doc->mergeXmlField() with an array
231
232         [field2.id] [field2.name]
233
234         $doc->mergeXmlField() with an object
235
236         [field3.getZipMethod]
237         [field3.getZipBinary]
238         [field3.getUnzipBinary]
239
240         $doc->mergeXmlBlock() with an array
241
242         [block1;block=begin][block1.$] [block1.firstname] [block1.lastname]
243         [block1;block=end]
244         Num rows : [block1.#]
245
246
247   * Save the document as `basicSuccess.ods` in template directory `doc/templates/`
248
249   * You can now test my OpenOffice calc document in a browser the result : [doc/basicCalc](http://tinydoc.unesolution.fr/doc/basicCalc)
250
251
252 ## and for Word2007 document (.docx)
253
254   * Change in your action
255
256         [php]
257         <?php
258         $doc->createFrom();
259         $doc->loadXml('content.xml');
260
261   By
262
263         [php]
264         <?php
265         $doc->createFrom(array('extension' => 'docx'));
266         $doc->loadXml('word/document.xml');
267
268   * Create an Word2007 document (.docx) and paste the same as before.
269
270         $doc->mergeXmlField() with a string
271
272         [field1]
273
274         $doc->mergeXmlField() with an array
275
276         [field2.id] [field2.name]
277
278         $doc->mergeXmlField() with an object
279
280         [field3.getZipMethod]
281         [field3.getZipBinary]
282         [field3.getUnzipBinary]
283
284         $doc->mergeXmlBlock() with an array
285
286         [block1;block=begin][block1.$] [block1.firstname] [block1.lastname]
287         [block1;block=end]
288         Num rows : [block1.#]
289
290
291   * Save the document as `basicSuccess.docx` in template directory `doc/templates/`
292
293   * You can now test the Word2007 document in a browser the result : [doc/basicWord](http://tinydoc.unesolution.fr/doc/basicWord)
294
295
296   ### Tips
297
298         [php]
299         <?php
300         $doc->createFrom();  // the defaut extension is 'odt'
301         $doc->loadXml();     // the defaut XML filename is 'content.xml'
302
303
304 ## The basic structure of OpenDocument (OpenOffice) and Word 2007 files
305
306   * The documents are a zip archive format with these main files :   
307
308                           OpenDocument      Word2007
309
310         content           content.xml       word/document.xml
311         meta              meta.xml          ?
312         settings          settings.xml      word/settings.xml
313         styles            styles.xml        word/styles.xml     
314         header            styles.xml        word/header1.xml     
315         footer            styles.xml        word/footer1.xml     
316
317   __`content.xml`__ is the main file to merge in OpenOffice documents.
318
319   __`word/document.xml`__ is the main file to merge in Word 2007 documents.
320
321   __`content.xml`__ is the defaut file in __`loadXml()`__ method.
322
323   See more on :
324
325     - [OpenOffice XML website](http://xml.openoffice.org/general.html)
326     - [OpenDocument XML website](http://opendocument.xml.org/faq)
327     - [OASIS OpenDocument Essentials](http://books.evc-cit.info/odbook/book.html)
328
329   * The XML tags you have to know :
330
331                     HTML tags     OpenDocument XML tags     Word2007 XML tags
332
333         table       <table>       <table:table>             <w:tbl>
334         row         <tr>          <table:table-row>         <w:tr>
335         cell        <td>          <table:table-cell>        <w:tc>
336         paragraph   <p>           <text:p>                  <w:p>
337
338
339 ## The method to merge data
340
341   * __Before merging data__, you have to load the XML file with the __`loadXml()`__ method.
342   * __After merging data__, you have to save the result of merging in the XML file with the __`saveXml()`__ method.
343
344   Each time you have to merge data with an another file you have to
345
346         [php]
347         <?php
348         $doc->loadXml('content.xml');
349         // ...
350         $doc->mergeXml(...);
351         $doc->mergeXmlField(...);
352         $doc->mergeXmlBlock(...);
353         $doc->mergeXml(...);
354         // ...
355         $doc->saveXml();
356
357
358         $doc->loadXml('styles.xml');
359         // ...
360         $doc->mergeXml(...);
361         $doc->mergeXmlField(...);
362         $doc->mergeXmlBlock(...);
363         $doc->mergeXml(...);
364         // ...
365         $doc->saveXml();
366
367   * There two different methods to merge data, field and block
368
369     * method : __`mergeXmlField($name, $data)`__
370
371       MergeField it's for fields (one time)
372
373     * method : __`mergeXmlBlock($name, $data)`__
374
375       MergeBlock it's for repetive fields to merge all items from an array.
376
377     > Note : If you don't define the begin block and the end of the block, the fields are merged one time.
378
379
380   * The two methods call the generic method : __`mergeXml($options, $data)`__
381
382   With this method, you can pass some parameters in the __`$options`__ array like :
383
384     * __name__:       The tag name in the template ('block' by default)
385     * __type__:       The tag type in the template ('field' | 'block' - 'block' by default)
386     * __data_type__:  The data type - only for type = 'block' ('array' by default)
387     * __charset__:    The data charset ('UTF-8' by default)
388     * __is_escape__:  If data are escaped (true by default)
389     * __callback__:   The callback to encode data ('=~encodeData' by default)
390
391     Example :
392
393           [php]
394           <?php
395           $doc->mergeXml(
396             array(
397               'name'      => 'b1',
398               'type'      => 'block',
399               'data_type' => 'array',
400               'charset'   => 'UTF-8',
401               'is_escape' => true,
402             ),
403             $data
404           );
405
406
407
408 ## TinyButStrong templates syntax (TBS tags)
409
410   For all parameters of TinyButStrong, have a look to the [TinyButStrong manual](http://www.tinybutstrong.com/manual.php#html_side)
411
412 ### The TBS field parameters
413
414   * ifempty
415   * if expr1=expr2;then val1;else val2
416   * frm=format1|format2|format3|format4
417   * magnet
418   * mtype
419   * ope=action
420
421 ### The TBS block parameters
422
423   * block
424   * $
425   * #
426   * nodata
427   * bmagnet
428   * headergrp
429   * footergrp
430   * splittergrp
431   * parentgrp
432   * serial
433   * p1=val1 (for subquery, this param don't work inside a class, like for sfTinyDoc)
434   * when expr1=expr2
435   * default
436   * several
437
438 ### New parameters for OpenOffice
439
440   * __image__ : This parameter is to merge image automaticaly
441  
442     * __image__ or __image=100%__ are equivalent
443     * __image=[ratio]__ fix the ratio of the merged image
444     * __image=fit__ the merged image is reduced or increased to fit on template's image size with the best ratio
445     * __image=max__ the merged image is limited to the maximum template's image size
446
447       Examples :
448
449             [field;image]
450             [field.src;image]
451             [block.src;image]
452             [block.src;image=50%]
453             [block.src;image=max]
454             [block.src;image=fit]
455
456     > Note #1 : The TBS tag is set in the image name. Use OpenOffice dialog box image property to edit.
457     >
458     > Note #2 : The file image is added into the document and you don't have to use the __`addFile()`__ method.
459     >
460     > Note #3 : If the image is empty or not found, the image is removed.
461
462
463   * __link__ : This parameter to transform to a linkable text
464
465     * __link__
466     * __link=click here__ to render a different text, "click here" in this example
467
468
469   * __type__ : This parameter fixes the __type__ of the cell, not the format (only for spreadsheet).
470
471     * __type=currency__   or the shorten way __type=c__. Input data is float.
472     * __type=date__       or __type=d__. Input data is string like 'Y-m-d' or 'Y-m-d H:i:s'.
473     * __type=number__     or __type=n__. Input data is float or integer.
474     * __type=percent__    or __type=p__. Input data is float.
475     * __type=time__       or __type=t__. Input data is string like 'H:i:s'.
476
477       Examples :
478
479             [field;type=number]
480             [field;type=n]
481             [field.data;type=date]
482             [block.data;type=percentage]
483             [block.data;type=time]
484
485     > Note #1 : __Only one TBS field__ can be merge in a cell when the parameter __`type`__ is set.
486     >
487     > Note #2 : To fix the __format__ the cell, use OpenOffice dialog box format for that. Don't use __`frm`__ parameter.
488
489
490
491
492 ## Examples
493
494 As in French we can said, "Un dessin vaut mieux qu'un long discours", here some [examples](http://tinydoc.unesolution.fr/examples) for sfTinyDocPlugin
495
496
497 ## Bugs
498
499 * news parameters [link, image and type] only work with data merged from an array
500
501
502 ## Features
503
504 * add tinyDocException class
505
506         [php]
507         <?php
508         class tinyDocException extends Exception
509         {
510         }
511
512 * add optionals parameters in method __`sendResponse()`__
513
514 * support 'inches' in parameter image
515
516 * add formula support in spreadsheet
517
518         [php]
519         <?php
520         <table:table-cell table:formula="of:=IF([.B2]&gt;0;&quot;true&quot;;&quot;false&quot;)" office:value-type="string" office:string-value="true">
521           <text:p>true</text:p>
522         </table:table-cell>
523
524
525 * support native format (float, percentage, currency, date, time) in word processing tables (* difficult)
526
527 * support merge images with a block in spreadsheet (* difficult)
528
529 * next TinyButStrong version (3.5 ?) to support iterators and a new parameter to merge sub-block.
530
531
532 ## Todo
533
534 - More documentation
535 - More examples on specials TBS parameters
536
537
538 ## Support
539
540 Mailing-list : [http://groups.google.fr/group/tinydoc](http://groups.google.fr/group/tinydoc)
541
542 Please email to the mailing-list [tinydoc@googlegroups.com](mailto:tinydoc@googlegroups.com) for support (register only).
543
544 Bug tracker : [http://trac.symfony-project.org/browser/plugins/sfTinyDocPlugin](http://trac.symfony-project.org/browser/plugins/sfTinyDocPlugin)
545
546 The __tbsOOo class__ is no longer supported, and I strongly discourage his use.
547
548
549 ## Contribution
550
551 If you have feature suggestions, bug reports, patches, usage examples for the documentation
552  or want to become an active contributor, send me an email to: olivierloynet [at] gmail [dot] com
553
554 Any help is welcome!
555
556
557 ## Acknowledgements
558
559 Special thanks to Vincent who write TinyButStrong.
560
561 To Romain, Pierre and Cecile.
Note: See TracBrowser for help on using the browser.