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

This example shows how to insert textual data into a template, stored as a DOCX file. The resulting word processing document is saved as a PDF file. The template, which can be downloaded below, is a software license agreement that contains the following 7 text fields:

  • software
  • licensee
  • company
  • date
  • time
  • city
  • country

Consider the following PHP5 code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$mailMerge = new Zend_Service_LiveDocx_MailMerge();
 
$mailMerge->setUsername('myUsername')
          ->setPassword('myPassword');
 
$mailMerge->setLocalTemplate('template.docx');
 
$mailMerge->assign('software', 'Magic Graphical Compression Suite v1.9')
          ->assign('licensee', 'Henry Döner-Meyer')
          ->assign('company',  'Co-Operation')
          ->assign('date',     'January 11, 2010')
          ->assign('time',     'January 11, 2010')
          ->assign('city',     'Berlin')
          ->assign('country',  'Germany');
 
$mailMerge->createDocument();
 
$document = $mailMerge->retrieveDocument('pdf');
 
file_put_contents('document.pdf', $document);

The logic is as follows:

  1. Initiate the phpLiveDocx object.
  2. Specify the name of the template file.
  3. One text field at a time, assign the data to the template.
  4. Create the document.
  5. Return the document – in this case as a PDF file – and store it in the variable $document.
  6. In a final step, the document is written to disk. It could have equally been stored in a database or sent via e-mail as an attachment.

And that is all there is to it: One of the core principles of phpLiveDocx is to embrace the extreme simplicity philosophy of the Zend Framework.

You can download the DOCX template file here:

And the resulting PDF document here:

The phpLiveDocx component, which is released under the New BSD License, has been written for the Zend Framework. You can download the component from the downloads section.

Leave a Reply