Read doc and docx files in Cake php

This post is intended to provide a reliable way to read the contents of .docx and .doc files. The content can be imported as html and inserted in ckeditor or any other such tool for further processing enabling users to edit the contents and save them.

The contents are read from the doc and docx file retaining their formatting.

How to read doc and docx file in php

  1. Downlaod the Zend library and put it in the vendors folder.
  2. We will be using the Zend Live Docx service for this process for which we need to create an account on https://www.livedocx.com/user/account_registration.aspx. Its free of cost.
  3. Next we need to create a file named zend_include_path.php file and put it in vendors folder.
  4. In our zend_include_path.php we will have to write following snippet

<?php

ini_set('include_path',ini_get('include_path') . PATH_SEPARATOR . dirname(__FILE__));

?>

5. Next, in our controller we need to write

<?php 
class ReadFileController extends AppController { 
  /** 
  * Function to read doc files using Zend Live Docx service and return the contents as html 
  * @author Ankit Tiwari 
  * @email ankit@artofcoding.in 
  * @param string $docPath The path of the file to read 
  * @return string the contents of the doc file in HTML format 
  */ 
  public function getImportedFileContent($docPath)
  { 
     App::import('Vendor', 'zend_include_path'); 
     App::import('Vendor', 'Zend_Service_LiveDocx_MailMerge', true, false, 'Zend/Service/LiveDocx/MailMerge.php' ); 
     App::import('Vendor', 'Zend_Service_LiveDocx_Exception', true, false, 'Zend/Service/LiveDocx/Exception.php' ); 
     // call the class mailmerge 
     $mailMerge = new Zend_Service_LiveDocx_MailMerge(); 
     // sets the login username and password here to access services 
     $mailMerge->setUsername($userName)
            ->setPassword($password);
     $mailMerge->setLocalTemplate($docPath); 
     // sets the docx file. 
     $mailMerge->createDocument();
     $document = $mailMerge->retrieveDocument('html');
     return $document;
    }

}