PHP
  Home arrow PHP arrow Page 5 - PHP and COM
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

PHP and COM
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2002-04-23


    Table of Contents:
  • PHP and COM
  • Striving To Excel
  • The Number Game
  • Export Potential
  • Keeping It Simple
  • Access-ing The Web
  • All For One, And One For All
  • New Coins For Old
  • Link Zone

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP and COM - Keeping It Simple
    ( Page 5 of 9 )

    As I'm sure you've figured out by now, converting a Word document to HTML with PHP isn't really as difficult as it sounds. I have no intention of wasting my weekend writing complex search-replace algorithms to perform this task. Instead, I'm going to make my life (and yours) a whole lot simpler by having a Microsoft Word COM object (and its built-in methods) take care of it for me.

    <?php // htmlviewer.php // convert a Word doc to an HTML file $DocumentPath = str_replace("\\\\", "\\", $DocumentPath); // create an instance of the Word application $word = new COM("word.application") or die("Unable to instantiate application object"); // creating an instance of the Word Document object $wordDocument = new COM("word.document") or die("Unable to instantiate document object"); // open up an empty document $wordDocument = $word->Documents->Open($DocumentPath); // create the filename for the HTML version $HTMLPath = substr_replace($DocumentPath, 'html', -3, 3); // save the document as HTML $wordDocument->SaveAs($HTMLPath, 8); // clean up $wordDocument = null; $word->Quit(); $word = null; // redirect the browser to the newly-created document header("Location:" . $HTMLPath); ?>
    As you can see, this is fairly simple, and quite similar to the script I wrote a few pages back for Microsoft Excel. Again, the first step is to use the COM extension to create an instance of the Microsoft Word application object, followed by an instance of the Word document object

    <?php // create an instance of the Word application $word = new COM("word.application") or die("Unable to instantiate application object"); ?>
    Once that's done, the next step is to open up the specified document in Word and use the object's SaveAs() method to save it as HTML.

    <?php // open up an empty document $wordDocument = $word->Documents->Open($DocumentPath); // create the filename for the HTML version $HTMLPath = substr_replace($DocumentPath, 'html', -3, 3); // save the document as HTML $wordDocument->SaveAs($HTMLPath, 8); ?>
    Note the second argument passed to the SaveAs() method, the integer 8 - this is a numeric code which tells Word to save the document as HTML. Feel free to experiment with this number and create different file formats - the Web page at http://msdn.microsoft.com/library/en-us/modcore/html/deovrWorkingWithMicrosoftWordObjects.asp has more information on the API for this object.

    Once that's done, all that's left is to clean up and redirect the Web browser to the specified HTML file via a call to header().

    <?php // clean up $wordDocument = null; $word->Quit(); $word = null; // redirect the browser to the newly-created document header("Location:" . $HTMLPath); ?>
    Note also the call to str_replace() at the top of the script; this is needed in order to create a valid Windows file path, and remove the extraneous escape characters (slashes) that PHP adds to the GET URL string.

    <?php $DocumentPath = str_replace("\\\\", "\\", $DocumentPath); ?>


     
     
    >>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek