Forum Moderators: coopster

Message Too Old, No Replies

PHP Creating Word Documents On-The-Fly

It's working, but when client saves opened file, prompted to save as HTML

         

Jeremy_H

3:42 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



I know this is a stretch to put this post in the PHP forum, but I do think this is the most appropriate place of all available options.

I'm trying to use PHP to create a word document on the fly.

I've read about various approaches, and have finally found one I feel is the fastest with the most options.

It seems to be working fine (lots of tweaks still left to work out), but there is one issue that is driving me crazy.

Even though I'm using PHP to send out the correct header, when the client tries to save an opened word file, they are prompted by default to save it in HTML, not as a word doc.

What should I do?

Thanks.

CODE:

<?php

header("Content-Type: application/msword");
header("Content-Disposition: attachment; filename=\"file.doc\"");

?>
<html xmlns:w="urn:schemas-microsoft-com:office:word">
<head>
<title><?php echo(rand());?></title>
<xml>
<o:DocumentProperties>
<o:Author><?php echo(rand());?></o:Author>
</o:DocumentProperties>
</xml>
<style>
h1{font:16pt arial,sans-serif}
p{font:12pt arial,sans-serif}
</style>
</head>

<body>
<div>
<h1><o:p><?php echo(rand());?></o:p></h1>
<p><o:p><?php echo(rand());?></o:p></p>
</div>
</body>
</html>

[edited by: Jeremy_H at 3:43 pm (utc) on Aug. 3, 2006]

coopster

8:35 pm on Aug 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't think you are going to find a way around it. Try it yourself, simulate the process outside of any web programming like this:
  1. Create a new text document with Notepad or any general text editor.
  2. Key in the exact same output here (replacing your PHP code entries with some random text).
  3. Save the file as text.
  4. Rename the file to have a .doc extension
  5. Open with MS Word
  6. Try "Save As ..."

You get prompted to save it as an HTML document. Just one of those wonderful MS ways, my friend ;)

Jeremy_H

7:58 pm on Aug 8, 2006 (gmt 0)

10+ Year Member



Thanks very much for the reply.

Since it is rather easy to create a Word Document (via the header), does anybody know of a layout scheme other than HTML I can use to create a word document?

For example, does word have its own markup language I could use to create a true, non web-based word document?

Thanks

jatar_k

3:06 pm on Aug 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for a good look at word formatting open a word file in a text editor