phpLiveDocx allows designers to insert any number of text fields into a template. These text fields are populated with data when createDocument() is called.
In addition to text fields, it is also possible specify regions of a document, which should be repeated.
For example, in a telephone bill it is necessary to print out a list of all connections, including the destination number, duration and cost of each call. This repeating row functionality can be achieved with so called blocks.
Blocks are simply regions of a document, which are repeated when createDocument() is called. In a block any number of block fields can be specified.
Blocks consist of two consecutive document targets with a unique name. The following screenshot illustrates these targets and their names in red:
The format of a block is as follows:
blockStart_ + unique name
blockEnd_ + unique name
The content of a block is repeated, until all data assigned in the block fields has been injected into the template.
Consider the following PHP5 code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
$mailMerge = new Zend_Service_LiveDocx_MailMerge(); $mailMerge->setUsername('myUsername') ->setPassword('myPassword'); $mailMerge->setLocalTemplate('template.doc'); $billConnections = array ( array ('connection_number' => '+49 421 335 912', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'), array ('connection_number' => '+49 421 335 913', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'), array ('connection_number' => '+49 421 335 914', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'), array ('connection_number' => '+49 421 335 916', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03') ); $mailMerge->assign('connection', $billConnections); $mailMerge->createDocument();
The data, which is specified in the array $billConnections is repeated in the template in the block connection. The keys of the array (connection_number, connection_duration and fee are the block field names – their data is inserted, one row per iteration.
You can download the DOC template file here:
- template.doc [20.5 KB]
And the resulting PDF document here:
- document.pdf [77.6 KB]
Note that blocks may not be nested.
To try out this example for yourself, download phpLiveDocx. It is contained in the directory /samples/mail-merge/telephone-bill/