Page 1 of 1
Creating MS Word document from PHP
Posted: Mon Jul 05, 2004 10:36 am
by pickle
Hi All,
Does anyone know how to make a Microsoft Word document in PHP?
Failing that, how would I go about putting a "tab" character in a text document that would be recognized by Word when it opens said document?
Thanks.
P.S. I tried searching the forums but "Microsoft" and "Word" are not the most limiting of search terms.

Posted: Mon Jul 05, 2004 10:40 am
by malcolmboston
look for "COM"
its also in the php.net manual somewhere however i cannot find it
i know though, it only works on microsoft systems (ie not linux hosts)
Posted: Mon Jul 05, 2004 10:48 am
by redmonkey
A tab character is represent by "\t" or "\x09".
You could try looking at OpenOffice as that can save 'Word compatible' files I would presume there is something about the file structure/format in there.
Posted: Mon Jul 05, 2004 10:50 am
by JayBird
here a little example of how to write to a word doc. You need to be on a windows server for this
Code: Select all
<?
$word=new COM("word.application") or die("Cannot start MS Word");
print "Loaded word version ($word->Version)\n";
$word->visible = 1 ;
$word->Documents->Add();
$word->Selection->Typetext("This is a test");
?>
Then read the COM functions in the manual -
http://se.php.net/manual/en/ref.com.php
You will also have to visit microsofts site to find out how to do all the writing and reading of the files. I have only used it for writing, but anything the user can do, you can do.
Mark
Posted: Mon Jul 05, 2004 10:51 am
by pickle
~malcolmboston: I saw that too, but I am working on a Linux box.
~redmonkey: Thanks for the tab character. I want to output a .doc file from a PHP generated webpage, not a gui word processor. Thanks though.
Posted: Mon Jul 05, 2004 10:56 am
by redmonkey
My suggestion is not that you use Openoffice but, as it has the ability to save as 'Word compatible' files then there is probably some comenting in the source with regard to the file structure/format they use.
From that you can work out how to write 'Word compatible' files.
Posted: Mon Jul 05, 2004 10:59 am
by pickle
Ah I see. Thanks (but I don't want to know that bad

)
Posted: Mon Jul 05, 2004 11:06 am
by redmonkey
You can always cheat!
Code: Select all
<?php
$fp = fopen('S:/sample.doc', 'w');
fwrite($fp, '<html><body><center><h1>Hello World!</h1></center><br>');
fwrite($fp, ' Some <b>text</b> and <i>some</i> more');
fclose($fp);
?>
Saving an HTML document as a .doc extension, works with Word2000 and up (or at least it did the last time I tried it.
You can also mimic Excel files using HTML tables!
Posted: Mon Jul 05, 2004 11:09 am
by pickle
Hmmm..... that might be worth looking into. Thanks.
Posted: Mon Jul 05, 2004 11:34 am
by launchcode
pickle - Word for a few versions now has saved its files in an XML format instead of the closed proprietary format Microsoft used to use a few years ago. There is book by O'Reilly dedicated to parsing the XML Office format, it might be worth taking out of the local library/etc? Can't remember the name off-hand, but it wouldn't take 10 seconds on Google to find it.