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

Please note: This post is part of a five post series. See introduction, part 1, part 2, part 3 and part 4.

The manufacturer of the celebrated word processing component TX Text Control has recently brought to market a document creation platform, which provides a very elegant solution to the programmatic creation of well-formatted word processing documents in PDF, DOCX, DOC and RTF formats. The platform, which is SOAP based and hence completely independent of any specific programming language, is called LiveDocx.

The PHP community has put together a PHP5 implementation, called phpLiveDocx, which is released under the New BSD License and can be downloaded from the downloads section of this blog.

The idea of phpLiveDocx is really simple: Indeed, it embodies the extreme simplicity philosophy of the Zend Framework, for which the component has been designed. Using phpLiveDocx, a graphical designer can create a template in Microsoft Word, Open Office or a specialized tool, called TX Template Designer. In the template, s/he can embed any number of text fields, into which, in a second step, data can be populated using phpLiveDocx. The resulting word processing document can be saved as a PDF, DOCX, DOC or RTF file.

Consider the following diagram – it explains the flow of data in a phpLiveDocx application:

The green shape to the left symbolizes a template. The dark blue shape directly beneath symbolizes arbitrary data (in this case, textual data from an XML file). The cloud in the center of the diagram is the LiveDocx service. The resulting document (in this case a very short letter) is symbolized by the light blue shape to the right.

Or, expressed as PHP5 code:

1
2
3
4
5
6
7
8
9
10
11
12
13
$mailMerge = new Zend_Service_LiveDocx_MailMerge();
 
$mailMerge->setUsername('myUsername')
          ->setPassword('myPassword');
 
$mailMerge->setLocalTemplate('template.docx');
 
$mailMerge->assign('firstname', 'Peter');
$mailMerge->assign('user', 'Jacky Hewitt');
 
$mailMerge->createDocument();
 
$document = $mailMerge->retrieveDocument('pdf');

The resulting word processing document, as a PDF file, is stored in the variable $document. Simply by passing docx, doc or rtf, it would have been equally possible to retrieve the document in another supported format.

Although the above example could have been implemented using Zend_Pdf – given that the developer has sufficient patience and time to get the positioning of the inserted text just right and that the output document is a PDF file – there are certain types of document formatting, which make the deployment of the Zend Framework component extremely tedious.

Consider the following template file, which is part of a software license agreement. Using this template, a software manufacturer can generate a license agreement for a specific software and licensee. The text fields are represented as %textFieldName%.

Agreement for %software% between %company% and %licensee%

Subject of agreement
The subject of this agreement is the software %software%, the operating manuals, online help files and all other accompanying material. It will be referred to henceforth as %software%.

Grant of license
%company% grants %licensee% a non-exclusive, non-transferable, personal and worldwide license to use one copy of %software% in the development of an end-user application, as described in section 3 (below). This license is for a single developer and not for an entire company. If additional programmers wish to use %software%, additional copies must be licensed.

In this template, there is the additional problem that it is impossible to know in advance exactly at which coordinates text should be inserted. The position of the text fields depends upon the length of the content in the previous text field. In the title of the agreement, for example, the value for %company% must be inserted at two entirely different coordinates, if %software% has the value Ace Downloader or Magical Compression Suite Deluxe Edition Pro. The resulting documents would look as follows:

Agreement for Ace Downloader between MegaSoft Corp and Peter Paul

and

Agreement for Magical Compress Suite Deluxe Edition Pro between MegaSoft Corp and Peter Paul

As the name of the first software has only 14 characters, %company% appears on the first line, whereas in the second example, %software% has 44 characters, hence pushing the second and third text field onto the next line.

Using phpLiveDocx, the developer does not have to worry about the plethora of possible layout and formatting options in a word processing document. All s/he needs to know is the names of the text fields. phpLiveDocx has been designed from the ground up to make the creation of word processing documents a pain-free process, thus increasing the productivity of the developer.

Continue to the next part of this five part series:

Leave a Reply