1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.service.livedocx">
4 <title>Zend_Service_LiveDocx</title>
6 <sect2 id="zend.service.livedocx.introduction">
7 <title>Introduction to LiveDocx</title>
10 LiveDocx is a <acronym>SOAP</acronym> service that allows developers to generate word
11 processing documents by combining structured data from <acronym>PHP</acronym> with a
12 template, created in a word processor. The resulting document can be saved as a
13 <acronym>PDF</acronym>, <acronym>DOCX</acronym>, <acronym>DOC</acronym>,
14 <acronym>HTML</acronym> or <acronym>RTF</acronym> file. LiveDocx implements <ulink
15 url="http://en.wikipedia.org/wiki/Mail_merge">mail-merge</ulink> in
16 <acronym>PHP</acronym>.
20 The family of <classname>Zend_Service_LiveDocx</classname> components provides a clean
21 and simple interface to the <ulink url="http://www.livedocx.com">LiveDocx API</ulink>
22 and additionally offers functionality to improve network performance.
26 In addition to this section of the manual, if you are interested in learning more about
27 <classname>Zend_Service_LiveDocx</classname> and the backend <acronym>SOAP</acronym>
28 service LiveDocx, please take a look at the following resources:
34 <emphasis>Shipped demonstration applications</emphasis>. There are a large
35 number of demonstration applications in the directory
36 <emphasis>/demos/Zend/Service/LiveDocx</emphasis> of the Zend Framework
37 distribution file or trunk version, checked out of the standard SVN repository.
38 These are designed to get you up to speed with
39 <classname>Zend_Service_LiveDocx</classname> within a matter of minutes.
45 <ulink url="http://www.phplivedocx.org/">
46 <classname>Zend_Service_LiveDocx</classname> blog and web site</ulink>.
52 <ulink url="http://www.livedocx.com/pub/documentation/api.aspx">
53 LiveDocx SOAP API documentation</ulink>.
59 <ulink url="https://api.livedocx.com/1.2/mailmerge.asmx?wsdl">
60 LiveDocx WSDL</ulink>.
66 <ulink url="https://www.livedocx.com/">LiveDocx blog and web site</ulink>.
71 <sect3 id="zend.service.livedocx.account">
72 <title>Sign Up for an Account</title>
75 Before you can start using LiveDocx, you must first <ulink
76 url="https://www.livedocx.com/user/account_registration.aspx">sign up</ulink>
77 for an account. The account is completely free of charge and you only need to
78 specify a <emphasis>username</emphasis>, <emphasis>password</emphasis> and
79 <emphasis>e-mail address</emphasis>. Your login credentials will be dispatched to
80 the e-mail address you supply, so please type carefully.
84 <sect3 id="zend.service.livedocx.templates-documents">
85 <title>Templates and Documents</title>
88 LiveDocx differentiates between the following terms: 1)
89 <emphasis>template</emphasis> and 2) <emphasis>document</emphasis>. In order to
90 fully understand the documentation and indeed the actual <acronym>API</acronym>, it
91 is important that any programmer deploying LiveDocx understands the difference.
95 The term <emphasis>template</emphasis> is used to refer to the input file, created
96 in a word processor, containing formatting and text fields. You can download an
98 url="http://www.phplivedocx.org/wp-content/uploads/2009/01/license-agreement-template.docx">example
99 template</ulink>, stored as a <acronym>DOCX</acronym> file. The term
100 <emphasis>document</emphasis> is used to refer to the output file that contains the
101 template file, populated with data - i.e. the finished document. You can download an
103 url="http://www.phplivedocx.org/wp-content/uploads/2009/01/license-agreement-document.pdf">example
104 document</ulink>, stored as a <acronym>PDF</acronym> file.
108 <sect3 id="zend.service.livedocx.formats">
109 <title>Supported File Formats</title>
112 LiveDocx supports the following file formats:
115 <sect4 id="zend.service.livedocx.formats.template">
116 <title>Template File Formats (input)</title>
119 Templates can be saved in any of the following file formats:
125 <ulink url="http://en.wikipedia.org/wiki/Office_Open_XML">DOCX</ulink> -
126 Office Open <acronym>XML</acronym> format
132 <ulink url="http://en.wikipedia.org/wiki/DOC_(computing)">DOC</ulink> -
133 Microsoft Word <acronym>DOC</acronym> format
139 <ulink url="http://en.wikipedia.org/wiki/Rich_Text_Format">RTF</ulink> -
140 Rich text file format
146 <ulink url="http://www.textcontrol.com/">TXD</ulink> - TX Text Control
153 <sect4 id="zend.service.livedocx.formats.document">
154 <title>Document File Formats (output):</title>
157 The resulting document can be saved in any of the following file formats:
163 <ulink url="http://en.wikipedia.org/wiki/Office_Open_XML">DOCX</ulink> -
164 Office Open <acronym>XML</acronym> format
170 <ulink url="http://en.wikipedia.org/wiki/DOC_(computing)">DOC</ulink> -
171 Microsoft Word <acronym>DOC</acronym> format
177 <ulink url="http://en.wikipedia.org/wiki/Xhtml">HTML</ulink> -
178 <acronym>XHTML</acronym> 1.0 transitional format
184 <ulink url="http://en.wikipedia.org/wiki/Rich_Text_Format">RTF</ulink> -
185 Rich text file format
192 url="http://en.wikipedia.org/wiki/Portable_Document_Format">PDF</ulink>
193 - Acrobat Portable Document Format
199 <ulink url="http://www.textcontrol.com/">TXD</ulink> - TX Text Control
206 <ulink url="http://en.wikipedia.org/wiki/Text_file">TXT</ulink> -
207 <acronym>ANSI</acronym> plain text
213 <sect4 id="zend.service.livedocx.formats.image">
214 <title>Image File Formats (output):</title>
217 The resulting document can be saved in any of the following graphical file
224 <ulink url="http://en.wikipedia.org/wiki/BMP_file_format">BMP</ulink> -
231 <ulink url="http://en.wikipedia.org/wiki/GIF">GIF</ulink> - Graphics
238 <ulink url="http://en.wikipedia.org/wiki/Jpg">JPG</ulink> - Joint
239 Photographic Experts Group format
246 url="http://en.wikipedia.org/wiki/Portable_Network_Graphics">PNG</ulink>
247 - Portable Network Graphics format
254 url="http://en.wikipedia.org/wiki/Tagged_Image_File_Format">TIFF</ulink>
255 - Tagged Image File Format
261 <ulink url="http://en.wikipedia.org/wiki/Windows_Metafile">WMF</ulink> -
262 Windows Meta File format
270 <sect2 id="zend.service.livedocx.mailmerge">
271 <title>Zend_Service_LiveDocx_MailMerge</title>
274 <classname>Zend_Service_LiveDocx_MailMerge</classname> is the mail-merge object in the
275 <classname>Zend_Service_LiveDocx</classname> family.
278 <sect3 id="zend.service.livedocx.mailmerge.generation">
279 <title>Document Generation Process</title>
282 The document generation process can be simplified with the following equation:
286 <emphasis>Template + Data = Document</emphasis>
290 Or expressed by the following diagram:
295 fileref="figures/zend.service.livedocx.mailmerge.generation-diabasic_zoom.png"
300 Data is inserted into template to create a document.
304 A template, created in a word processing application, such as Microsoft Word, is
305 loaded into LiveDocx. Data is then inserted into the template and the resulting
306 document is saved to any supported format.
310 <sect3 id="zend.service.livedocx.mailmerge.templates">
311 <title>Creating Templates in Microsoft Word 2007</title>
314 Start off by launching Microsoft Word and creating a new document. Next, open up the
315 <emphasis>Field</emphasis> dialog box. This looks as follows:
320 fileref="figures/zend.service.livedocx.mailmerge.templates-msworddialog_zoom.png"
325 Microsoft Word 2007 Field dialog box.
329 Using this dialog, you can insert the required merge fields into your document.
330 Below is a screenshot of a license agreement in Microsoft Word 2007. The merge
331 fields are marked as <code>{ MERGEFIELD FieldName }</code>:
336 fileref="figures/zend.service.livedocx.mailmerge.templates-mswordtemplatefull_zoom.png"
341 Template in Microsoft Word 2007.
345 Now, save the template as <emphasis>template.docx</emphasis>.
349 In the next step, we are going to populate the merge fields with textual data from
350 <acronym>PHP</acronym>.
355 fileref="figures/zend.service.livedocx.mailmerge.templates-mswordtemplatecropped_zoom.png"
360 Cropped template in Microsoft Word 2007.
364 To populate the merge fields in the above cropped screenshot of the <ulink
365 url="http://www.phplivedocx.org/wp-content/uploads/2009/01/license-agreement-template.docx">template</ulink>
366 in Microsoft Word, all we have to code is as follows:
369 <programlisting language="php"><![CDATA[
370 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
372 $phpLiveDocx->setUsername('myUsername')
373 ->setPassword('myPassword');
375 $phpLiveDocx->setLocalTemplate('template.docx');
377 $phpLiveDocx->assign('software', 'Magic Graphical Compression Suite v1.9')
378 ->assign('licensee', 'Henry Döner-Meyer')
379 ->assign('company', 'Co-Operation');
381 $phpLiveDocx->createDocument();
383 $document = $phpLiveDocx->retrieveDocument('pdf');
385 file_put_contents('document.pdf', $document);
389 The resulting document is written to disk in the file
390 <emphasis>document.pdf</emphasis>. This file can now be post-processed, sent via
391 e-mail or simply displayed, as is illustrated below in <emphasis>Document Viewer
392 2.26.1</emphasis> on <emphasis>Ubuntu 9.04</emphasis>:
397 fileref="figures/zend.service.livedocx.mailmerge.templates-msworddocument_zoom.png"
402 Resulting document as <acronym>PDF</acronym> in Document Viewer 2.26.1.
406 <sect3 id="zend.service.livedocx.mailmerge.advanced">
407 <title>Advanced Mail-Merge</title>
410 <classname>Zend_Service_LiveDocx_MailMerge</classname> allows designers to insert
411 any number of text fields into a template. These text fields are populated with data
412 when <emphasis>createDocument()</emphasis> is called.
416 In addition to text fields, it is also possible specify regions of a document, which
421 For example, in a telephone bill it is necessary to print out a list of all
422 connections, including the destination number, duration and cost of each call. This
423 repeating row functionality can be achieved with so called blocks.
427 <emphasis>Blocks</emphasis> are simply regions of a document, which are repeated
428 when <methodname>createDocument()</methodname> is called. In a block any number of
429 <emphasis>block fields</emphasis> can be specified.
433 Blocks consist of two consecutive document targets with a unique name. The following
434 screenshot illustrates these targets and their names in red:
439 fileref="figures/zend.service.livedocx.mailmerge.advanced-mergefieldblockformat_zoom.png"
444 The format of a block is as follows:
447 <programlisting language="text"><![CDATA[
448 blockStart_ + unique name
449 blockEnd_ + unique name
452 <para>For example:</para>
454 <programlisting language="text"><![CDATA[
460 The content of a block is repeated, until all data assigned in the block fields has
461 been injected into the template. The data for block fields is specified in
462 <acronym>PHP</acronym> as a multi-assoc array.
466 The following screenshot of a template in Microsoft Word 2007 shows how block fields
472 fileref="figures/zend.service.livedocx.mailmerge.advanced-mswordblockstemplate_zoom.png"
477 Template, illustrating blocks in Microsoft Word 2007.
481 The following code populates the above template with data.
484 <programlisting language="php"><![CDATA[
485 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
487 $phpLiveDocx->setUsername('myUsername')
488 ->setPassword('myPassword');
490 $phpLiveDocx->setLocalTemplate('template.doc');
492 $billConnections = array(
494 'connection_number' => '+49 421 335 912',
495 'connection_duration' => '00:00:07',
499 'connection_number' => '+49 421 335 913',
500 'connection_duration' => '00:00:07',
504 'connection_number' => '+49 421 335 914',
505 'connection_duration' => '00:00:07',
509 'connection_number' => '+49 421 335 916',
510 'connection_duration' => '00:00:07',
515 $phpLiveDocx->assign('connection', $billConnections);
517 // ... assign other data here ...
519 $phpLiveDocx->createDocument();
520 $document = $phpLiveDocx->retrieveDocument('pdf');
521 file_put_contents('document.pdf', $document);
525 The data, which is specified in the array <varname>$billConnections</varname> is
526 repeated in the template in the block connection. The keys of the array
527 (<varname>connection_number</varname>, <varname>connection_duration</varname> and
528 <varname>fee</varname>) are the block field names - their data is inserted, one row
533 The resulting document is written to disk in the file
534 <emphasis>document.pdf</emphasis>. This file can now be post-processed, sent via
535 e-mail or simply displayed, as is illustrated below in <emphasis>Document Viewer
536 2.26.1</emphasis> on <emphasis>Ubuntu 9.04</emphasis>:
541 fileref="figures/zend.service.livedocx.mailmerge.advanced-mswordblocksdocument_zoom.png"
546 Resulting document as <acronym>PDF</acronym> in Document Viewer 2.26.1.
550 You can download the <acronym>DOC</acronym> <ulink
551 url="http://www.phplivedocx.org/wp-content/uploads/2009/01/telephone-bill-template.doc">template
552 file</ulink> and the resulting <ulink
553 url="http://www.phplivedocx.org/wp-content/uploads/2009/01/telephone-bill-document.pdf">PDF
558 <emphasis>NOTE:</emphasis> blocks may not be nested.
562 <sect3 id="zend.service.livedocx.mailmerge.bitmaps">
563 <title>Generating bitmaps image files</title>
566 In addition to document file formats,
567 <classname>Zend_Service_LiveDocx_MailMerge</classname> also allows documents to be
568 saved to a number of image file formats (<acronym>BMP</acronym>,
569 <acronym>GIF</acronym>, <acronym>JPG</acronym>, <acronym>PNG</acronym> and
570 <acronym>TIFF</acronym>). Each page of the document is saved to one file.
574 The following sample illustrates the use of <methodname>getBitmaps($fromPage,
575 $toPage, $zoomFactor, $format)</methodname> and
576 <methodname>getAllBitmaps($zoomFactor, $format)</methodname>.
580 <varname>$fromPage</varname> is the lower-bound page number of the page range that
581 should be returned as an image and <varname>$toPage</varname> the upper-bound page
582 number. <varname>$zoomFactor</varname> is the size of the images, as a percent,
583 relative to the original page size. The range of this parameter is 10 to 400.
584 <varname>$format</varname> is the format of the images returned by this method. The
585 supported formats can be obtained by calling
586 <methodname>getImageFormats()</methodname>.
589 <programlisting language="php"><![CDATA[
590 $date = new Zend_Date();
591 $date->setLocale('en_US');
593 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
595 $phpLiveDocx->setUsername('myUsername')
596 ->setPassword('myPassword');
598 $phpLiveDocx->setLocalTemplate('template.docx');
600 $phpLiveDocx->assign('software', 'Magic Graphical Compression Suite v1.9')
601 ->assign('licensee', 'Daï Lemaitre')
602 ->assign('company', 'Megasoft Co-operation')
603 ->assign('date', $date->get(Zend_Date::DATE_LONG))
604 ->assign('time', $date->get(Zend_Date::TIME_LONG))
605 ->assign('city', 'Lyon')
606 ->assign('country', 'France');
608 $phpLiveDocx->createDocument();
611 // (zoomFactor, format)
612 $bitmaps = $phpLiveDocx->getAllBitmaps(100, 'png');
614 // Get just bitmaps in specified range
615 // (fromPage, toPage, zoomFactor, format)
616 // $bitmaps = $phpLiveDocx->getBitmaps(2, 2, 100, 'png');
618 foreach ($bitmaps as $pageNumber => $bitmapData) {
619 $filename = sprintf('documentPage%d.png', $pageNumber);
620 file_put_contents($filename, $bitmapData);
625 This produces two files (<filename>documentPage1.png</filename> and
626 <filename>documentPage2.png</filename>) and writes them to disk in the same
627 directory as the executable <acronym>PHP</acronym> file.
632 fileref="figures/zend.service.livedocx.mailmerge.bitmaps-documentpage1_zoom.png"
642 fileref="figures/zend.service.livedocx.mailmerge.bitmaps-documentpage2_zoom.png"
651 <sect3 id="zend.service.livedocx.mailmerge.templates-types">
652 <title>Local vs. Remote Templates</title>
655 Templates can be stored <emphasis>locally</emphasis>, on the client machine, or
656 <emphasis>remotely</emphasis>, on the server. There are advantages and disadvantages
661 In the case that a template is stored locally, it must be transfered from the client
662 to the server on every request. If the content of the template rarely changes, this
663 approach is inefficient. Similarly, if the template is several megabytes in size, it
664 may take considerable time to transfer it to the server. Local template are useful
665 in situations in which the content of the template is constantly changing.
669 The following code illustrates how to use a local template.
672 <programlisting language="php"><![CDATA[
673 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
675 $phpLiveDocx->setUsername('myUsername')
676 ->setPassword('myPassword');
678 $phpLiveDocx->setLocalTemplate('./template.docx');
680 // assign data and create document
684 In the case that a template is stored remotely, it is uploaded once to the server
685 and then simply referenced on all subsequent requests. Obviously, this is much
686 quicker than using a local template, as the template does not have to be transfered
687 on every request. For speed critical applications, it is recommended to use the
688 remote template method.
692 The following code illustrates how to upload a template to the server:
695 <programlisting language="php"><![CDATA[
696 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
698 $phpLiveDocx->setUsername('myUsername')
699 ->setPassword('myPassword');
701 $phpLiveDocx->uploadTemplate('template.docx');
705 The following code illustrates how to reference the remotely stored template on all
709 <programlisting language="php"><![CDATA[
710 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
712 $phpLiveDocx->setUsername('myUsername')
713 ->setPassword('myPassword');
715 $phpLiveDocx->setRemoteTemplate('template.docx');
717 // assign data and create document
721 <sect3 id="zend.service.livedocx.mailmerge.information">
722 <title>Getting Information</title>
725 <classname>Zend_Service_LiveDocx_MailMerge</classname> provides a number of methods
726 to get information on field names, available fonts and supported formats.
729 <example id="zend.service.livedocx.mailmerge.information.getfieldname">
730 <title>Get Array of Field Names in Template</title>
733 The following code returns and displays an array of all field names in the
734 specified template. This functionality is useful, in the case that you create an
735 application, in which an end-user can update a template.
738 <programlisting language="php"><![CDATA[
739 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
741 $phpLiveDocx->setUsername('myUsername')
742 ->setPassword('myPassword');
744 $templateName = 'template-1-text-field.docx';
745 $phpLiveDocx->setLocalTemplate($templateName);
747 $fieldNames = $phpLiveDocx->getFieldNames();
748 foreach ($fieldNames as $fieldName) {
749 printf('- %s%s', $fieldName, PHP_EOL);
754 <example id="zend.service.livedocx.mailmerge.information.getblockfieldname">
755 <title>Get Array of Block Field Names in Template</title>
758 The following code returns and displays an array of all block field names in the
759 specified template. This functionality is useful, in the case that you create an
760 application, in which an end-user can update a template. Before such templates
761 can be populated, it is necessary to find out the names of the contained block
765 <programlisting language="php"><![CDATA[
766 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
768 $phpLiveDocx->setUsername('myUsername')
769 ->setPassword('myPassword');
771 $templateName = 'template-block-fields.doc';
772 $phpLiveDocx->setLocalTemplate($templateName);
774 $blockNames = $phpLiveDocx->getBlockNames();
775 foreach ($blockNames as $blockName) {
776 $blockFieldNames = $phpLiveDocx->getBlockFieldNames($blockName);
777 foreach ($blockFieldNames as $blockFieldName) {
778 printf('- %s::%s%s', $blockName, $blockFieldName, PHP_EOL);
784 <example id="zend.service.livedocx.mailmerge.information.getfontnames">
785 <title>Get Array of Fonts Installed on Server</title>
788 The following code returns and displays an array of all fonts installed on the
789 server. You can use this method to present a list of fonts which may be used in
790 a template. It is important to inform the end-user about the fonts installed on
791 the server, as only these fonts may be used in a template. In the case that a
792 template contains fonts, which are not available on the server,
793 font-substitution will take place. This may lead to undesirable results.
796 <programlisting language="php"><![CDATA[
797 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
799 $phpLiveDocx->setUsername('myUsername')
800 ->setPassword('myPassword');
802 Zend_Debug::dump($phpLiveDocx->getFontNames());
806 <emphasis>NOTE:</emphasis> As the return value of this method changes very
807 infrequently, it is highly recommended to use a cache, such as
808 <classname>Zend_Cache</classname> - this will considerably speed up your
813 <example id="zend.service.livedocx.mailmerge.information.gettemplateformats">
814 <title>Get Array of Supported Template File Formats</title>
817 The following code returns and displays an array of all supported template file
818 formats. This method is particularly useful in the case that a combo list should
819 be displayed that allows the end-user to select the input format of the
820 documentation generation process.
823 <programlisting language="php"><![CDATA[
824 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge()
826 $phpLiveDocx->setUsername('myUsername')
827 ->setPassword('myPassword');
829 Zend_Debug::dump($phpLiveDocx->getTemplateFormats());
833 <emphasis>NOTE:</emphasis> As the return value of this method changes very
834 infrequently, it is highly recommended to use a cache, such as
835 <classname>Zend_Cache</classname> - this will considerably speed up your
840 <example id="zend.service.livedocx.mailmerge.information.getdocumentformats">
841 <title>Get Array of Supported Document File Formats</title>
844 The following code returns and displays an array of all supported document file
845 formats. This method is particularly useful in the case that a combo list should
846 be displayed that allows the end-user to select the output format of the
847 documentation generation process.
850 <programlisting language="php"><![CDATA[
851 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
853 $phpLiveDocx->setUsername('myUsername')
854 ->setPassword('myPassword');
856 Zend_Debug::dump($phpLiveDocx->getDocumentFormats());
860 <example id="zend.service.livedocx.mailmerge.information.getimageformats">
861 <title>Get Array of Supported Image File Formats</title>
864 The following code returns and displays an array of all supported image file
865 formats. This method is particularly useful in the case that a combo list should
866 be displayed that allows the end-user to select the output format of the
867 documentation generation process.
870 <programlisting language="php"><![CDATA[
871 $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
873 $phpLiveDocx->setUsername('myUsername')
874 ->setPassword('myPassword');
876 Zend_Debug::dump($phpLiveDocx->getImageFormats());
880 <emphasis>NOTE:</emphasis> As the return value of this method changes very
881 infrequently, it is highly recommended to use a cache, such as
882 <classname>Zend_Cache</classname> - this will considerably speed up your
890 vim:se ts=4 sw=4 tw=100 et: