Guest User

phplivedocx

a guest
Jan 24th, 2010
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Example of how to convert word processing documents, using phpLiveDocx.
  5.  *
  6.  * Supported input formats  : doc, docx, rtf & txd.
  7.  * Supported output formats : doc, docx, html, pdf, rtf, txd & txt.
  8.  *
  9.  * In a future version of phpLiveDocx, this functionality will be made available
  10.  * directly in the core API. For the time being, this class offers document
  11.  * conversion with the current version of phpLiveDocx (v1.2).
  12.  *
  13.  * NOTE 1: This class accesses the constants USERNAME and PASSWORD. These are
  14.  *         your credentials to the phpLiveDocx service and must have been defined
  15.  *         before the class can work. e.g.
  16.  *
  17.  *         define ('USERNAME', 'myUsername');
  18.  *         define ('PASSWORD', 'myPassword');
  19.  *
  20.  * NOTE 2: The code in this class is for exemplary purposes only. For example,
  21.  *         Zend_Service_LiveDocx_MailMerge::setLocalTemplate() and
  22.  *         Zend_Service_LiveDocx_MailMerge::createDocument() throw exceptions
  23.  *         should something go wrong. In production code, you should therefore
  24.  *         use these methods in a try..catch block. This has been left out here
  25.  *         to aide debugging and to make the example code as simple as possible.
  26.  */
  27. class Converter
  28. {
  29.     /**
  30.      * Convert a word processing document from one format to another
  31.      *
  32.      * @param string $filename File to convert
  33.      * @param string $format   Format into which to convert (doc, docx, html, pdf, rtf, txd or txt)
  34.      * @return binary
  35.      */
  36.     public static function convert($filename, $format)
  37.     {
  38.         $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  39.  
  40.         $phpLiveDocx->setUsername(USERNAME)
  41.                     ->setPassword(PASSWORD);
  42.  
  43.         $phpLiveDocx->setLocalTemplate($filename);
  44.         $phpLiveDocx->assign('dummyField', 'dummyValue');
  45.         $phpLiveDocx->createDocument();
  46.  
  47.         return $phpLiveDocx->retrieveDocument($format);
  48.     }
  49.  
  50.     /**
  51.      * Helper method to return the filename of the converted document
  52.      *
  53.      * @param string $filename File to convert
  54.      * @param string $format   Format into which to convert (docx, doc, rtf, txd, pdf, txt)
  55.      * @return string
  56.      */
  57.     public static function getFilename($filename, $format)
  58.     {
  59.         $pattern = sprintf('\.%s$', Zend_Service_LiveDocx::getFormat($filename));
  60.         $replace = '.' . $format;
  61.  
  62.         return preg_replace("/{$pattern}/", $replace, $filename);
  63.     }
  64. }
  65.  
Add Comment
Please, Sign In to add comment