Written by: Jonathan Maron.

Zend_Service_LiveDocx allows developers to generate documents by combining structured data from PHP with a template, created in a word processor. The resulting document can be saved as a PDF, DOCX, DOC or RTF file. The concept is the same as with mail-merge.

  1. Load template in Zend_Service_LiveDocx.
  2. Assign data to text fields in template.
  3. Generate document.
  4. Save as a PDF, DOCX, DOC or RTF file.

Consider this example: A template is populated with the following data to generate this document.

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);

Want to try this out? Take a look at the getting started guide for details or contact the author.