LiveDocx: Create DOCX, DOC, RTF and PDF

6 comments

Just a quick post to share with you a service I found over the weekend.  The site is called LiveDocx, and with a free membership you get all the tools you need to be able to generate DOCX, DOC, RTF and PDF documents on the fly from templates!

Microsoft has always included components in their tools to do something called a ‘mail merge’.  Say you want to mail out a form letter to 1000 contacts, but don’t have the time to customize each letter by including the persons name or address.  That is what a mail merge has traditionally been for.  Define a few fields in your Word document and let the program do the substitution for you automatically!

The LiveDocx service works in this way.  You create a template in one of the supported formats (DOCX or DOC) and upload it into your account on their servers.  Armed with the template, and a SOAP based API you can tap into this service using PHP.

There are limits to the service… for example you cannot create documents on the fly.  If you had fields in your document you wanted left out if they were empty you would be out of luck.  Still, the service provided by these guys is a great tool to accomplish some simple mail merge like functionality without all the usual hassles associated with working in Microsoft files (OLE).

Code Sample

The PHPLiveDocx code recommends you make use of the Zend library, which in my case added a lot of extra file overhead I didn’t think was necessary.  After reading a few articles and doing some experimenting on my own I was able to eliminate the need for Zend by borrowing a couple functions from the PHPLiveDocx code and placing them directly in my own..  here is a sample.

function assocArrayToArrayOfArrayOfString($assoc) {
   $arrayKeys = array_keys($assoc);
   $arrayValues = array_values($assoc);
 
   return array($arrayKeys, $arrayValues);
}
 
function multiAssocArrayToArrayOfArrayOfString($multi) {
   $arrayKeys = array_keys($multi[0]);
   $arrayValues = array();
 
   foreach ($multi as $v) {
      $arrayValues[] = array_values($v);
   }
 
   $_arrayKeys = array();
   $_arrayKeys[0] = $arrayKeys;
 
   return array_merge($_arrayKeys, $arrayValues);
}
 
$USERNAME = "your username on the LiveDocx site";
$PASSWORD = "your password on the LiveDocx site";
$ENDPOINT = "https://api.livedocx.com/1.2/mailmerge.asmx?WSDL";
 
// Turn off WSDL caching
ini_set ('soap.wsdl_cache_enabled', 0);
 
// Define timezone
date_default_timezone_set('America/Toronto');
 
$soap = new SoapClient($ENDPOINT);
 
$soap->LogIn(
   array(
      'username' => $USERNAME,
      'password' => $PASSWORD
   )
);
 
$data = file_get_contents("your word template file");
 
$soap->SetLocalTemplate(
   array(
      'template' => base64_encode($data),
      'format'   => 'docx'  // change this to whatever your template is (i.e. doc/docx)
   )
);
 
// the data to populate the template with.  the fieldnames here should match the ones
// you defined in your template!
$data = array(
   'fieldname' => 'value',
   'fieldname2' => 'value2'
);
 
$soap->SetFieldValues(
   array (
      'fieldValues' => assocArrayToArrayOfArrayOfString($data)
   )
);
 
// Build the document
$soap->CreateDocument();
 
// Get document as PDF
$result = $soap->RetrieveDocument(
   array(
      'format' => strtoupper('PDF')
   )
);
 
$data = $result->RetrieveDocumentResult;
 
file_put_contents("OUTPUT_FILE", base64_decode($data));
 
$soap->LogOut();
unset($soap);

For the above code to work, you’re going to need the SOAP extension enabled in PHP. Some people have managed to get around this requirement by using the NuSOAP extension, but lucky for me I didn’t have to go that route.

Creating Your Template

I was using Word 2007 to create my template.  It took me a while to figure out how to insert the mail merge fields…

  1. Select the area of the document you want the data inserted into
  2. From the Insert menu, under Quick Parts, select Field
  3. Under Category choose ‘Mail Merge’ and under Field Names choose ‘MergeField’
  4. Give your field a name (this name is what you use to insert data as shown in the PHP code above)

Your mail merge fields will show up in the document like «fieldname».

September 8th, 2009 at 9:25 am  

Posted in Discoveries, PHP

6 Responses to 'LiveDocx: Create DOCX, DOC, RTF and PDF'

Subscribe to comments with RSS or TrackBack to 'LiveDocx: Create DOCX, DOC, RTF and PDF'.

  1. Hi James,

    Nice write-up of the whole LiveDocx topic. Also, thank you for testing the web service in the first place!

    I am not quite sure, what you mean by If you had fields in your document you wanted left out if they were empty you would be out of luck. If you want fields to disappear, just merge them with empty strings. Or am I missing something?

    Regards,
    Lucas

    Lucas

    8 Sep 09 at 11:16 am

  2. Hi Lucas! Do you work for LiveDocx? Let me commend you on the great service!

    As for the leaving fields out… in my case I had several headings in my document, bolded, larger font. I use the mailmerge fields for the paragraph under each heading. In a case like this leaving the field blank doesn’t look right as the header is still there.

    Another example would be a table in a Word document. If I wanted to dynamically populate rows, or remove rows I don’t end up populating, I’m not sure this could be accomplished.

    Are there ways to apply formatting like bold and font size into the text I merge into the document? And looking at tables, any way to create/remove cells and rows on the fly?

    fdask

    8 Sep 09 at 11:55 am

  3. Hi again,

    Thanks for exemplifying! I think, I can help you cope with these issues.

    Regarding the headings, let me point you to two articles I wrote a while ago:
    http://blog.livedocx.com/post/Using-INCLUDETEXT-Fieldse28093Part-1-Creating-Templates.aspx
    http://blog.livedocx.com/post/Using-INCLUDETEXT-Fieldse28093Part-2-Putting-It-All-To-Work.aspx

    Those articles explain how to use sub-templates to create your documents. By simply setting IgnoreSubTemplates(bool ignore) you can determine whether to include or exclude your sub-templates during the document creation process. Thus, you can easily omit a part of your template if you don’t have any data for it.

    As for tables, there is the concept of merge blocks. Simply think of a merge block as if it was a foreach loop. Firstly, you mark an area, e.g. a table row, of your template as merge block. Then, you submit a couple of datasets for that block, start the merging process and get a document that contains the submitted data iteratively merged into it. (I hope, this is correct English. Please bear with me, I’m German.)

    The LiveDocx MailMerge API does not – and will never – support merging formatted text into templates nor will it support applying formatting documents afterwards. We are working on expanding the LiveDocx service family with some more services. One of which will be used to work with formatted text.

    Last but not least, thank you for your kind words! I am indeed working for The Imaging Source, the company behind LiveDocx. It really makes me happy and proud to see the service, I helped building, is of use to somebody.

    Thanks,
    Lucas

    Lucas

    9 Sep 09 at 9:39 am

  4. Thanks for the follow up Lucas. Based on your comments, I guess I was looking at the service the wrong way. Rather than using the LiveDocx service itself to do the complex tasks like omitting empty fields, etc, I should be using some of the Word mail merge features.

    I’ll look into this and hopefully add some new code samples into my post! Cheers.

    fdask

    9 Sep 09 at 5:34 pm

  5. Hi,

    LiveDocx is a really good tool for creating documents.
    The only problem is that the free-service is often so overloaded, that you need to buy the premium-service if you want to use it. It’s a pity. I hope they can fix this.

    Regards Ron

    Ron Brendow

    16 Dec 09 at 2:29 am

  6. Hey Ron,

    While I don’t use the service heavily (no more than once or twice a week), I have never encountered any issues with it! What sort of things are you experiencing? And have you emailed the company about them?

    Having a barely useable free version that serves as nothing more than a funnel to a paid service is a bad way to operate. Totally agree with you.

    fdask

    16 Dec 09 at 5:59 am

Leave a Reply