Zend_Service_Flickr

Zend_Service_LiveDocx

Introduction to LiveDocx

LiveDocx is a SOAP service that allows developers to generate word processing 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, HTML or RTF file. LiveDocx implements » mail-merge in PHP.

The family of Zend_Service_LiveDocx components provides a clean and simple interface to the » LiveDocx API and additionally offers functionality to improve network performance.

In addition to this section of the manual, if you are interested in learning more about Zend_Service_LiveDocx and the backend SOAP service LiveDocx, please take a look at the following resources:

Sign Up for an Account

Before you can start using LiveDocx, you must first » sign up for an account. The account is completely free of charge and you only need to specify a username, password and e-mail address. Your login credentials will be dispatched to the e-mail address you supply, so please type carefully.

Templates and Documents

LiveDocx differentiates between the following terms: 1) template and 2) document. In order to fully understand the documentation and indeed the actual API, it is important that any programmer deploying LiveDocx understands the difference.

The term template is used to refer to the input file, created in a word processor, containing formatting and text fields. You can download an » example template, stored as a DOCX file. The term document is used to refer to the output file that contains the template file, populated with data - i.e. the finished document. You can download an » example document, stored as a PDF file.

Supported File Formats

LiveDocx supports the following file formats:

Template File Formats (input)

Templates can be saved in any of the following file formats:

  • » DOCX - Office Open XML format

  • » DOC - Microsoft Word DOC format

  • » RTF - Rich text file format

  • » TXD - TX Text Control format

Document File Formats (output):

The resulting document can be saved in any of the following file formats:

  • » DOCX - Office Open XML format

  • » DOC - Microsoft Word DOC format

  • » HTML - XHTML 1.0 transitional format

  • » RTF - Rich text file format

  • » PDF - Acrobat Portable Document Format

  • » TXD - TX Text Control format

  • » TXT - ANSI plain text

Image File Formats (output):

The resulting document can be saved in any of the following graphical file formats:

  • » BMP - Bitmap image format

  • » GIF - Graphics Interchange Format

  • » JPG - Joint Photographic Experts Group format

  • » PNG - Portable Network Graphics format

  • » TIFF - Tagged Image File Format

  • » WMF - Windows Meta File format

Zend_Service_LiveDocx_MailMerge

Zend_Service_LiveDocx_MailMerge is the mail-merge object in the Zend_Service_LiveDocx family.

Document Generation Process

The document generation process can be simplified with the following equation:

Template + Data = Document

Or expressed by the following diagram:

zend.service.livedocx.mailmerge.generation-diabasic_zoom.png

Data is inserted into template to create a document.

A template, created in a word processing application, such as Microsoft Word, is loaded into LiveDocx. Data is then inserted into the template and the resulting document is saved to any supported format.

Creating Templates in Microsoft Word 2007

Start off by launching Microsoft Word and creating a new document. Next, open up the Field dialog box. This looks as follows:

zend.service.livedocx.mailmerge.templates-msworddialog_zoom.png

Microsoft Word 2007 Field dialog box.

Using this dialog, you can insert the required merge fields into your document. Below is a screenshot of a license agreement in Microsoft Word 2007. The merge fields are marked as { MERGEFIELD FieldName }:

zend.service.livedocx.mailmerge.templates-mswordtemplatefull_zoom.png

Template in Microsoft Word 2007.

Now, save the template as template.docx.

In the next step, we are going to populate the merge fields with textual data from PHP.

zend.service.livedocx.mailmerge.templates-mswordtemplatecropped_zoom.png

Cropped template in Microsoft Word 2007.

To populate the merge fields in the above cropped screenshot of the » template in Microsoft Word, all we have to code is as follows:

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

The resulting document is written to disk in the file document.pdf. This file can now be post-processed, sent via e-mail or simply displayed, as is illustrated below in Document Viewer 2.26.1 on Ubuntu 9.04:

zend.service.livedocx.mailmerge.templates-msworddocument_zoom.png

Resulting document as PDF in Document Viewer 2.26.1.

Advanced Mail-Merge

Zend_Service_LiveDocx_MailMerge 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:

zend.service.livedocx.mailmerge.advanced-mergefieldblockformat_zoom.png

The format of a block is as follows:

  1. blockStart_ + unique name
  2. blockEnd_ + unique name

For example:

  1. blockStart_block1
  2. blockEnd_block1

The content of a block is repeated, until all data assigned in the block fields has been injected into the template. The data for block fields is specified in PHP as a multi-assoc array.

The following screenshot of a template in Microsoft Word 2007 shows how block fields are used:

zend.service.livedocx.mailmerge.advanced-mswordblockstemplate_zoom.png

Template, illustrating blocks in Microsoft Word 2007.

The following code populates the above template with data.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. $phpLiveDocx->setLocalTemplate('template.doc');
  7.  
  8. $billConnections = array(
  9.     array(
  10.         'connection_number'   => '+49 421 335 912',
  11.         'connection_duration' => '00:00:07',
  12.         'fee'                 => '€ 0.03',
  13.     ),
  14.     array(
  15.         'connection_number'   => '+49 421 335 913',
  16.         'connection_duration' => '00:00:07',
  17.         'fee'                 => '€ 0.03',
  18.     ),
  19.     array(
  20.         'connection_number'   => '+49 421 335 914',
  21.         'connection_duration' => '00:00:07',
  22.         'fee'                 => '€ 0.03',
  23.     ),
  24.     array(
  25.         'connection_number'   => '+49 421 335 916',
  26.         'connection_duration' => '00:00:07',
  27.         'fee'                 => '€ 0.03',
  28.     ),
  29. );
  30.  
  31. $phpLiveDocx->assign('connection', $billConnections);
  32.  
  33. // ... assign other data here ...
  34.  
  35. $phpLiveDocx->createDocument();
  36. $document = $phpLiveDocx->retrieveDocument('pdf');
  37. file_put_contents('document.pdf', $document);

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.

The resulting document is written to disk in the file document.pdf. This file can now be post-processed, sent via e-mail or simply displayed, as is illustrated below in Document Viewer 2.26.1 on Ubuntu 9.04:

zend.service.livedocx.mailmerge.advanced-mswordblocksdocument_zoom.png

Resulting document as PDF in Document Viewer 2.26.1.

You can download the DOC » template file and the resulting » PDF document.

NOTE: blocks may not be nested.

Generating bitmaps image files

In addition to document file formats, Zend_Service_LiveDocx_MailMerge also allows documents to be saved to a number of image file formats (BMP, GIF, JPG, PNG and TIFF). Each page of the document is saved to one file.

The following sample illustrates the use of getBitmaps($fromPage, $toPage, $zoomFactor, $format) and getAllBitmaps($zoomFactor, $format).

$fromPage is the lower-bound page number of the page range that should be returned as an image and $toPage the upper-bound page number. $zoomFactor is the size of the images, as a percent, relative to the original page size. The range of this parameter is 10 to 400. $format is the format of the images returned by this method. The supported formats can be obtained by calling getImageFormats().

  1. $date = new Zend_Date();
  2. $date->setLocale('en_US');
  3.  
  4. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  5.  
  6. $phpLiveDocx->setUsername('myUsername')
  7.             ->setPassword('myPassword');
  8.  
  9. $phpLiveDocx->setLocalTemplate('template.docx');
  10.  
  11. $phpLiveDocx->assign('software', 'Magic Graphical Compression Suite v1.9')
  12.             ->assign('licensee', 'Daï Lemaitre')
  13.             ->assign('company''Megasoft Co-operation')
  14.             ->assign('date',     $date->get(Zend_Date::DATE_LONG))
  15.             ->assign('time',     $date->get(Zend_Date::TIME_LONG))
  16.             ->assign('city',     'Lyon')
  17.             ->assign('country''France');
  18.  
  19. $phpLiveDocx->createDocument();
  20.  
  21. // Get all bitmaps
  22. // (zoomFactor, format)
  23. $bitmaps = $phpLiveDocx->getAllBitmaps(100, 'png');
  24.  
  25. // Get just bitmaps in specified range
  26. // (fromPage, toPage, zoomFactor, format)
  27. // $bitmaps = $phpLiveDocx->getBitmaps(2, 2, 100, 'png');
  28.  
  29. foreach ($bitmaps as $pageNumber => $bitmapData) {
  30.     $filename = sprintf('documentPage%d.png', $pageNumber);
  31.     file_put_contents($filename, $bitmapData);
  32. }

This produces two files (documentPage1.png and documentPage2.png) and writes them to disk in the same directory as the executable PHP file.

zend.service.livedocx.mailmerge.bitmaps-documentpage1_zoom.png

documentPage1.png.

zend.service.livedocx.mailmerge.bitmaps-documentpage2_zoom.png

documentPage2.png.

Local vs. Remote Templates

Templates can be stored locally, on the client machine, or remotely, on the server. There are advantages and disadvantages to each approach.

In the case that a template is stored locally, it must be transfered from the client to the server on every request. If the content of the template rarely changes, this approach is inefficient. Similarly, if the template is several megabytes in size, it may take considerable time to transfer it to the server. Local template are useful in situations in which the content of the template is constantly changing.

The following code illustrates how to use a local template.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. $phpLiveDocx->setLocalTemplate('./template.docx');
  7.  
  8. // assign data and create document

In the case that a template is stored remotely, it is uploaded once to the server and then simply referenced on all subsequent requests. Obviously, this is much quicker than using a local template, as the template does not have to be transfered on every request. For speed critical applications, it is recommended to use the remote template method.

The following code illustrates how to upload a template to the server:

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. $phpLiveDocx->uploadTemplate('template.docx');

The following code illustrates how to reference the remotely stored template on all subsequent requests:

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. $phpLiveDocx->setRemoteTemplate('template.docx');
  7.  
  8. // assign data and create document

Getting Information

Zend_Service_LiveDocx_MailMerge provides a number of methods to get information on field names, available fonts and supported formats.

Example #1 Get Array of Field Names in Template

The following code returns and displays an array of all field names in the specified template. This functionality is useful, in the case that you create an application, in which an end-user can update a template.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. $templateName = 'template-1-text-field.docx';
  7. $phpLiveDocx->setLocalTemplate($templateName);
  8.  
  9. $fieldNames = $phpLiveDocx->getFieldNames();
  10. foreach ($fieldNames as $fieldName) {
  11.     printf('- %s%s', $fieldName, PHP_EOL);
  12. }

Example #2 Get Array of Block Field Names in Template

The following code returns and displays an array of all block field names in the specified template. This functionality is useful, in the case that you create an application, in which an end-user can update a template. Before such templates can be populated, it is necessary to find out the names of the contained block fields.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. $templateName = 'template-block-fields.doc';
  7. $phpLiveDocx->setLocalTemplate($templateName);
  8.  
  9. $blockNames = $phpLiveDocx->getBlockNames();
  10. foreach ($blockNames as $blockName) {
  11.     $blockFieldNames = $phpLiveDocx->getBlockFieldNames($blockName);
  12.     foreach ($blockFieldNames as $blockFieldName) {
  13.         printf('- %s::%s%s', $blockName, $blockFieldName, PHP_EOL);
  14.     }
  15. }

Example #3 Get Array of Fonts Installed on Server

The following code returns and displays an array of all fonts installed on the server. You can use this method to present a list of fonts which may be used in a template. It is important to inform the end-user about the fonts installed on the server, as only these fonts may be used in a template. In the case that a template contains fonts, which are not available on the server, font-substitution will take place. This may lead to undesirable results.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. Zend_Debug::dump($phpLiveDocx->getFontNames());

NOTE: As the return value of this method changes very infrequently, it is highly recommended to use a cache, such as Zend_Cache - this will considerably speed up your application.

Example #4 Get Array of Supported Template File Formats

The following code returns and displays an array of all supported template file formats. This method is particularly useful in the case that a combo list should be displayed that allows the end-user to select the input format of the documentation generation process.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge()
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. Zend_Debug::dump($phpLiveDocx->getTemplateFormats());

NOTE: As the return value of this method changes very infrequently, it is highly recommended to use a cache, such as Zend_Cache - this will considerably speed up your application.

Example #5 Get Array of Supported Document File Formats

The following code returns and displays an array of all supported document file formats. This method is particularly useful in the case that a combo list should be displayed that allows the end-user to select the output format of the documentation generation process.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. Zend_Debug::dump($phpLiveDocx->getDocumentFormats());

Example #6 Get Array of Supported Image File Formats

The following code returns and displays an array of all supported image file formats. This method is particularly useful in the case that a combo list should be displayed that allows the end-user to select the output format of the documentation generation process.

  1. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  2.  
  3. $phpLiveDocx->setUsername('myUsername')
  4.             ->setPassword('myPassword');
  5.  
  6. Zend_Debug::dump($phpLiveDocx->getImageFormats());

NOTE: As the return value of this method changes very infrequently, it is highly recommended to use a cache, such as Zend_Cache - this will considerably speed up your application.


Zend_Service_Flickr