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:
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:
- Initiate the phpLiveDocx object.
- Specify the name of the template file.
- One text field at a time, assign the data to the template.
- Create the document.
- Return the document – in this case as a PDF file – and store it in the variable $document.
- 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:
- template.docx [46.7 KB]
And the resulting PDF document here:
- document.pdf [104.7 KB]
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.