phpword-2007livedocx

Merge field for image not displaying in Word 2007 using livedocx and php


I'm trying to add a server side image uploaded from a user form to a Word document that gets generated using LiveDocx.

My word template looks like this.

«image:photo»

AKA

{ MERGEFIELD image:photo \* MERGEFORMAT }

My php looks like this.

$mailMerge = new Zend_Service_LiveDocx_MailMerge();
$mailMerge->uploadImage($this->logo_path);
$mailMerge->assign('image:photo', $this->logo_path);

I just get a blank area where the image should be. My other merge fields are working properly.


Solution

  • I didn't realize that LiveDocx stores only the name of the file to be referenced. I found this out using:

    $mailMerge->listImages();
    

    The format came back like this:

    array
    0 => array
      'filename' => string 'directory_logo.png' (length=18)
      'fileSize' => int 12829
      'createTime' => int 1352835686
      'modifyTime' => int 1352835686
    

    So the template file was fine with this format:

    «image:photo»
    

    AKA

    { MERGEFIELD image:photo \* MERGEFORMAT }
    

    But my php needed to look like this:

    $mailMerge->uploadImage($this->logo_path);
    $mailMerge->assign('image:photo', $this->logo_file);
    

    My full working code looks like this:

    $mailMerge = new Zend_Service_LiveDocx_MailMerge();
    $mailMerge->setUsername($username)
      ->setPassword($password);
    $mailMerge->setLocalTemplate($template_path . '/service_template.docx');
    $mailMerge->uploadImage($this->logo_path);
    $mailMerge->assign('image:photo', 'directory_logo.png');
    $mailMerge->createDocument();
    $document = $mailMerge->retrieveDocument('docx');
    file_put_contents($this->config->livedocx->document . '/' . $this->prefs['serverid'] . '/service_directory.docx', $document);
    $mailMerge->deleteImage('directory_logo.png');