23
Jan
Written by: Jonathan Maron. Stored in: Samples.

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:

Block fields

The format of a block is as follows:

blockStart_ + unique name
blockEnd_ + unique name

For example:

blockStart_block1
blockEnd_block1

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:

And the resulting PDF document here:

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/

7 Responses to “More advanced phpLiveDocx example”

  1. oxygen Says:

    How do you add a ‘block’ to a word document?

  2. Jonathan Maron Says:

    This procedure is described in the blog post above. May I ask, with which part are you having difficulties. Thank you.

  3. Tcasco Says:

    I think he is asking about how do you insert and find/see the “blockstart_connection”/end in Word, because the fields are clear but the block start and end can not be seen in the doc file.

  4. Florante Says:

    Hi,

    Is it possible to mail merge image using phplivedocx? Can you show an example?

    Thanks.

    Florante

  5. Jonathan Maron Says:

    Hello Florante

    Currently, this is not possible. However, it is by far one of the most frequently requested features and will be implemented in a future version.

    For details, please take a look at this thread in the ‘feature request’ forum:

    http://www.theimagingsourceforums.com/showthread.php?t=321766

    Jonathan Maron

  6. james cooke Says:

    Oxygen,

    I couldn’t work out that either! But it appears they aren’t visible as they are bookmarks. You can add the blockstart_connection/end in Word by adding the bookmarks like so:

    1 – Select where you want the bookmark to go
    2 – Insert > Bookmark > Type name (blockstart_connection) and click ‘Add’
    3 – Repeat for end bookmark

    Job done :)

    Cheers,

    James

  7. tn Says:

    Really, Jonathan Maron? How to add ‘blocks’ (actually, bookmarks) to a word document is outlined in the blog post? Where? Thank you james cooke, for actually making a helpful post.

Leave a Reply