I am trying to let users enter various information onto my site and then let them export it into Word...I am able to get the exporting down fine and automatically open the Word Doc in Word, but Im not sure how to do formatting. Using html tags in the PHP echo does not work...Any ideas?

Here is what i have...


$FileName="NewFile". ".doc";

header("Content-type: application/x-ms-download"); 

header("Content-Disposition: attachment; filename=$FileName");

header('Cache-Control: public');

echo "This is a new Word Document";


Edited by greatunknown: n/a

6 Years
Discussion Span
Last Post by pbcomput

This is an interesting one, if i have some spare time tomorrow i'll look into it.

Btw, whats the reason for wanting to export it into a word doc and not say a PDF.


Well the main reason is because Im trying to create a Resume Builder for a school website and I want users to be able to export it to Word so they can customize and manipulate it as they wish. So I basically just need to export it to Word with very basic formatting.

Actually if, in the PHP echo statement at the bottom of my code, if I echo it with actually return keys such as this...


echo "First Name
      Last Name

      Paragraph 1 content

      Paragraph 2 content




...it actually does output the right formatting in word, so that is very helpful, but I dont know where to start with changing font size and styles and so forth...


I have had no problem with formatting. Word correctly picks up the html formatting and uses it. One thing that I did different from you was the header statement on line 5. I used the following:

header("Content-type: application/msword");

Give it a try see if it makes any difference.

Edited by chrishea: n/a


Well I switch the header statement and it first showed the tags in the word document but then I tried echoing the ENTIRE html page that I wanted Word to read, with html tags wrapping the echo (before I didnt have html tags, just random br and hr tags), and it read it like I wanted it, but it seemed like it opened in word wierd, like it knew it was getting html and asked if it wanted word to be the default web editor, does this happen to you???


In my case it just opens in Word like a normal Word document. It defaults to saving it as a web page but you can select from the list and save it as a normal Word document. The version of Word and the Browser you are using might have some impact. I'm using Word 2000.


When i run it i get an error saying converter not installed etc and then my html tags are shown as they would in your editor.

I'm using microsoft office word 2007.

As all three of us have had different experiences running this script i think that an accessibility issue may be a concern.

I'd pick a format that all versions of word will understand as well as most other readers, when i switch between windows and linux my files normally take the form of an RTF file.

Might be an idea to convert to a RTF file which in turn can be read by Word, Openoffice writer etc.


Word has various format handlers, some of which are optional. According to this list from Microsoft (for Word 2003), the HTML converter is standard. It's possible that it was optional in other versions.

Even if you specify the format to be RTF, if Word is your default program for handling RTF files you may hit the same problem. If your wish is to be able to work with any version of any word processor whether or not they have the (optional) format handlers installed, then you need to produce native RTF format and the path that we have been going down in this post (generating html and letting Word do the conversion) won't work). Personally, I'd rather let Word do the work for me but if your intent is to provide the code to a wide audience, then you're right that there could be some accessibility issues so, at a minimum, you might need to define some minimum requirements.



I have same problem. I want to open user's uploaded .doc,.docx,.txt or .pdf as html (open in browser)

I can open pdf without doing any extra coding. but i can't open other formates.

I used above given script by greatunknown. but its not working for me. or i am doing any mistake in it. I want to open files when user click on the link "Show File" at that time file will be open in browser.

Please help me to sort out this problem.

Thanks in advance. :)


You should be starting a new thread. Your problem isn't the same since this original thread was about downloading and you are asking about uploading.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.