Generate MSword DOC

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
popper
Forum Newbie
Posts: 3
Joined: Thu Nov 18, 2004 2:51 am

Generate MSword DOC

Post by popper »

Hello everybody
how can i generate ms doc files using php? (version doesnt matter)
i 've tried the following code:

Code: Select all

header("Content-type: application/msword");
header('Content-Disposition: attachment; filename="my.doc";'); 
echo "Helloooooooooooooooooo";
But it doesn't work. Do i have to change something particular in apache/php.ini config files?
i search the site but i find only parser to generate html from doc file, i need the opposite procedure. :?

thanks all,
bye :wink:

Davide
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

search the forum for ms word.
I've done it for you
viewtopic.php?t=27003

It has a link to where you can read about php and creating word documents.

phpScott
popper
Forum Newbie
Posts: 3
Joined: Thu Nov 18, 2004 2:51 am

Post by popper »

10x man, i've searched only in the site :oops:.

now it work, i've used this code

Code: Select all

?php
// Word execution
$word = new COM("word.application") or die("Non sono riuscito ad eseguire Word");
$word->Visible = 1;

//open empty doc 
$word->Documents->Add();

//text operations 
$word->Selection->TypeText("formattazione lettera blalblallb\n\n nome cliente "  . $_POSTїtesto]);
$word->Documentsї1]->SaveAs("e:\webroot\ciao.doc");

//close Word
$word->Quit();

//send to browser 
$word->Release();
$word = null;
header("Content-type: application/msword"); 
header('Content-Disposition: attachment; filename="ciao.doc"');
readfile("ciao.doc");
?>
this script create a doc file, using the text passed by a post form.
Then it send the file to the browser for downloading/opening.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

What happens if this script is executed by more than one person at the same time?

a possible scenario

person1: write document e:\foo.doc
person2:write document e:\foo.doc
person1: recieve e:\foo.doc

<-- concurrency problem! save the documents using a unique filename
popper
Forum Newbie
Posts: 3
Joined: Thu Nov 18, 2004 2:51 am

Post by popper »

this is a test code, only to try if ms doc generation works. In "final version" ill'put some synchronization, doc's file name probably will be determinated by unique mysql's table key.
Post Reply