Connecting Tech Pros Worldwide Forums | Help | Site Map

Converting HTML content to word doc using PHP code

Newbie
 
Join Date: Nov 2007
Posts: 1
#1: Nov 12 '07
Hi,

I want to Convert a HTML content to word doc using PHP code. pls help

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Nov 12 '07

re: Converting HTML content to word doc using PHP code


Hi. Welcome to TSDN!

The simplest way to create a Word document is to simply use the Filesystem Functions to create a .doc file containing HTML markup.

The other, slightly more complex way, is to use COM objects to create an actual Word Document. In order for this method to work, you need to have Word installed on the server. Then you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Create a Word object
  3. $word = new COM("word.application") or die ("Couldn't create a Word object");
  4. $word->visible = 1;
  5.  
  6. # Create a document and add some text
  7. $word->Documents->Add();
  8. $word->Selection->TypeText("This document was created in PHP!");
  9.  
  10. # Save the document and release the Word object
  11. $word->Documents[1]->SaveAs("sampleword.doc");
  12. $word->Quit();
  13. $word->Release();
  14. $word = null;
  15. ?>
  16.  
Note that Microsoft recommends against using Office in this manner. See this article for more on that.

The last and most complex method would be to create your own Word parser. To do this you would need pretty detailed info on the inner workings of a Word Document.
Newbie
 
Join Date: Nov 2007
Posts: 1
#3: Nov 12 '07

re: Converting HTML content to word doc using PHP code


Hi,

Quote:

Originally Posted by Atli

The last and most complex method would be to create your own Word parser. To do this you would need pretty detailed info on the inner workings of a Word Document.

I've often wondered about a 4th way: create OO.o XML (this should be relatively easy), and then callling OpenOffice to re-save the XML as .doc I've never gotten around to trying this, but it should not be hard..
It would, I think, be the easiest way if you want extensive formatting in .doc....

Anybody tried this?

greetz,

Paul
Reply