Creating word processing document using Zend_Service_LiveDocx
You may have heard about FPDF, TCPDF. These are used to generate PDF document in PHP. But very few of you may have heard about LiveDocx, can be found here http://www.livedocx.com/. The most cool news I’m going to tell you that Zend Framework 1.10.0 provides clean and simple interface to LiveDocx API. LiveDocx is soap based service. If you are interested more in LiveDocx, following the link I’ve already provided you.
In this article I am gonna tell you how to create word processing documents and PDF using Zend_Service_LiveDocx. I am sure you will find it very helpful.
You can create you desired document by following these two steps.
1) Create template file
2) Fetch records from your database, as you usually do and assigned them to the template.
1) Creating template file
You can create your template file using Microsoft Word, openOffice and save then as .doc or docx or .rtf format. Keep in mind that creating template file in MS Word and openOffice may differ. In my example I am using Microsoft Word.
Open Microsoft word and create new document.
Before creating the document, I want to show you the resultant template that I’ve created for this example is bellow.
The sign “I” at the stat and end means that I want to repeat this block many time.
And the result file, populated with the data will like this.
Now as you have opened and created document, move forward and insert table as I have inserted in the above example. Put [ in the first row and then click “Insert” in you menu bar and click “Field”. This will open up the following dialog.
Select “Mail Merge” from the “Categories” dropdown and select “MergeField” as show in the following screenshot.
in the “Field name” textbox write “pkey” as show in the following diagram.
Insert all your “MergeField” in the document and create the document you desired. Now if you want to repeat entire block you’ve created or part of it, click at the beginning of that block and select “Insert” form your menu bar and click “Bookmark” as shown in the following diagram.
This will open the following dialog.
I’ve written “blockstart_issue” in the “Bookmark name” text field and pressed “Add” button. Now go to the end of the document or the block you want to repeat, and insert another bookmark using the same method discussed above. But this time the name must be “blockend_issue”. The start bookmark must be like “blockstart_example” and end bookmark should be named “blockend_example”. You can place any text in place of “example” as I’ve put “issue” in my case.
Now as you have defined your template, save it at appropriate place.
2) Fetch records from you database, as we usually do and assigned them to the template.
You have successfully created template file, now its time to instantiate Zend_Service_LiveDocx in your controller as
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
$phpLiveDocx->setUsername("username")
->setPassword("password");
Keep in mind that Zend_Service_LiveDocx is based on soap service, so you will need to get register at http://www.livedocx.com/. When you register with the username and password you will need to set your username and password in place of username and password in theabove two lines.
Next step is to assign values to “MergeField”, you defined while creating your template file. This can simply be done as
$phpLiveDocx->assign(‘pkey’,’TC-4’);
But as I have put bookmarks at the start and end of the entire block, it means that I want that the entire block be repeated for me. So I will first create an associative array as
$issuesData = array( array ( ‘pkey’ => ‘TC-6’, ‘summary’ => ‘Pagination not working in administration area’, ‘created’ => ‘2010/2/12’ ………. ……. ) array ( ‘pkey’ => ‘TC-7’, ‘summary’ => ‘Validation not working’, ‘created’ => ‘2010/2/12’ ………. ……. ) );
And now assign this associative array as
$phpLiveDocx->assign(‘issue’, $issuesData);
Here issue is what we defined at the time of putting merge fileds “blockstart_issue” and “blockend_issue”
When you assign data to your “MergeField”, you will need to retrieve the desired document by the following statements.
$document = $phpLiveDocx->retrieveDocument('doc');
unset($phpLiveDocx);
Once the document is generated using the above statement, defined appropriate header and print the created document. I’m using the following statement.
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=report.doc");
print $document;
Note: Bookmark will not be visible when you put them in your template. In order to view them select “Tools” from menu bar and then “Options”. Check bookmark checkbox in the view tab in the popup dialog.








[...] Creating word processing document using Zend_Service_LiveDocx var addthis_pub = 'phplivedocx'; var addthis_language = 'en';var addthis_header_background = '#efefef';var addthis_header_color = '#1a1a1a';var addthis_options = 'email, favorites, digg, delicious, stumbleupon, google, myspace, facebook, reddit, live, misterwong, more'; No Comments [...]
Pingback by Zend_Service_LiveDocx Intro By Faheem Abbas — February 15, 2010 @ 9:55 am