Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating MS Word Document (*.doc) using PHP 2

Status
Not open for further replies.

sharapov

MIS
May 28, 2002
106
US
Is it possible to generate Microsoft Word file using PHP?

Let's say I am showing contents of MySQL database for the users on the page. I want my users to select several records and press a button. When they press a button I want a "Save as . . ." box to pop up so they could save the records that they selected in the Microsoft Word format (*.doc) to their machine.

I DON'T want to use COM objects and make my newly generated document to open in MS Word (I want to eliminate possibility that user doesn't have MS Word Installed on their machine). All I want for the user to save this file as MS Word to their computer.

Can this be done? (I know it can be done using ASP) If yes how?
Example of how it can be done will be very appreciated.
 
If this is script is running on a web server, then it won't matter if the client has Word installed or not. You're using the server's installed COM objects, and Bastien's recommendation will work. ______________________________________________________________________
TANSTAAFL!
 
I DO NOT want to use COM objects (read original post). I am not using Windows server, I am running UNIX.
 
YOU DID NOT say that you were running on Unix, regardless of the number of ALL CAPS you use.

I did read your original post. You said "I don't want to use COM objects" from which one can reasonably infer that the use of COM objects is a possibility, from which one can reasonably infer that your server is running Win32. As both Bastien and I did. If you had said "I can't run COM objects because my server is *nix", then you would said what you meant.

I don't know about producing a DOC file, but here's a couple of tidbits for producing an RTF:

This gives the basics for producing an RTF:

Many linux systems also have an app called sgml2rtf which will convert SGML to RTF. Something like it may be available on your system. ______________________________________________________________________
TANSTAAFL!
 
in order for unix to run any win32 app (like word) you have to install samba


hth Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Bastien,

Not samba, you mean wine, don't you? ______________________________________________________________________
TANSTAAFL!
 
I don't want to use win32 app on UNIX, I just want to generate *.doc or *.rtf documents using PHP!!!
 
shaparov,

You have made that point abundantly clear. My clarification and dogo1984's were directed at Bastien. Remember, you are not the only one who will ever read this thread. Suppose the next guy trying to solve this problem likes the idea of using Mi¢ro$oft code?

If it's that important and you have $45 to spend to solve it, look here: ______________________________________________________________________
TANSTAAFL!
 
sleipnir214,

Thanks for clearing it out. I was afraid that people didn't really understand what I am trying to do.

Anyway, I found that website as well. I am just surprised that there is no other FREE
smiletiniest.gif
solution available for this.
 
sharapov,

Have you ever looked at the RTF spec? It's gawdawful. If I had a need to produce RTF documents on the fly in PHP, I'd probably pony up the bucks just so that I wouldn't have to deal with the details of the format.

Another possibility is to use PHP to call perl scripts to produce the RTF document. Take a look in CPAN at for a perl class to produce the documents. The documentation for the class includes the RTF Cookbook, which might give you some ideas for brewing your own PHP class. ______________________________________________________________________
TANSTAAFL!
 
Header('Content-type: application/msword');
Header('Content-Disposition: attachment; filename=NA.doc');

This is what I use.... works great and avoids COM.

I spit out regular HTML (MS Word 97 or newer has no problem at all rendering it).. the Header('Content-type: application/msword'); forces word to open it.

This works for excel as well.
Header('Content-type: application/msexcel');
Header('Content-Disposition: attachment; filename=NA.xls');

with excel though each cell is determined by your table tags.

example:
<table>
<tr><td>1</td><td>2</td></tr>
</table>

in excel looks like:
|-------|
|_1_|_2_|

hope that helps
=================================
Imagination is more important than knowledge.
(A.E.)
 
Lynux,

Are you populating those MS Word/Excel from database?
Do you mind posting an example how can it be done?

Thanks
 
<?php
//------- Header Information you need------//
Header(&quot;Content-type: application/msword&quot;);
Header(&quot;Content-Disposition: attachment; filename=na.doc&quot;);
//----- Database connection-------//
$db = mysql_connect(&quot;localhost&quot;, &quot;login&quot;,&quot;pass&quot;);
mysql_select_db(&quot;database&quot;,$db);
//-------output data from database-----------//
$data = mysql_query(&quot;select field from table&quot;,$db);
while ($datAR= mysql_fetch_array($data)){
echo $datAR[field].&quot;<br>&quot;;
}
?> =================================
Imagination is more important than knowledge.
(A.E.)
 
Lynux,

Thanks for your help. Is there anyway to transfer images to word this way. I have names of the images stored in mySQL database, but I want those images to appear in the word along with data. Is it posible.

Thannks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top