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.

    You miss understand what you are doing with COM objects (if I am reading your question right). Rembmer PHP is done server side. So inorder for you to create a DOC file all you need is the server that you are running on to be running windows. Once the file is created, on the server, the file can then be sent to the client. However, if you are using this as some generic script that you might eventually put on a *nix box or some other OS then you are correct in the fact that you need to use something other than COM.

    That all being said. You can create wordfiles on Windows servers running PHP with COM, but cant create DOC files on any other machine. COM support for PHP hasnt been ported to *nix systems.
    COM is about the only way to create word files in PHP, without just decoding the thing yourself. 🙂

    Hope this Helps!

      RTF is different from DOC. Have you every tried to open a DOC file in a standard text viewer (ie. notepad in windows or vi in *nix) You will notice a lot of null character padding and a lot of weird data. A doc file cannot (unless you really know what you are doing) just be edited by a simple text editor. RTF is clear text. I can edit it with a simple text editor. So it would be realitivly "simple" to create a RTF file once you knew what the RTF syntax was. I have not ever used RTF, but from the dynamically generated file I saw, it looks like a windows rip off of TEX.
      . So to answer your question again. No you need to use COM objects, and there are currently no libraries in PHP that will decode DOC documents.

      Hope this Helps!

        8 months later

        Opening a word doc and saving it as HTML will save it as XML. Then it can be read using the XML capabilities of PHP.

        Martin

          The easy way to do this is to generate an RTF doc as stated below, but use http headers to fool the browser into thinking it is a word doc. When it is opened, Word is very forgiving and will open the rtf with all formatting as if it's a Word doc without complaint.

          header example:

          
          header("Content-disposition: filename=your_file.doc");
          header("Content-type: application/msword";
          
          

          ps. nashirak you sound like you have been programmed to give that response. Actually there are other ways to do it too. Using the java() function, you'd have a non-COM, free and platform independent way of doing it also. Check out http://jakarta.apache.org/poi/index.html for more info.

            Write a Reply...