Creating MS Word document from PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Creating MS Word document from PHP

Post 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. :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ah I see. Thanks (but I don't want to know that bad :) )
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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, '&nbsp;&nbsp;&nbsp;&nbsp;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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmmm..... that might be worth looking into. Thanks.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
Post Reply