3

in my site the admin edits some data which i need to save to a doc file. so i implemented this using ckeditor. It works fine but when i try to open this file it says "word cannot start the converter mswrd632.wpc". what am i doing wrong?

$content=addslashes(trim($_REQUEST['CKEditor']))
$docfile="convert.doc";
$fp = fopen("files/".$docfile, "w+");
fwrite($fp, $content);                                                           

this is my code how can we save data to a doc file.is there any other way?

<p class="body">
    England would be keen to finish the summer on a high note by also remaining unbeaten in the upcoming ODI series against world champions India, said Test skipper Andrew Strauss after handing out the visitors a 4-0 whitewash.</p>
Sibu
  • 4,609
  • 2
  • 26
  • 38
  • 2
    What you are doing is not generating a doc file, you are masking a HTML file as a Word file in the hopes that it will auto-import it. Just to clarify. You will probably need to show how your HTML starts – Pekka Oct 15 '11 at 10:02
  • File types not only differ in their entension. You have to write the data in the doc format, so word can interpret the data. What you saved is basically a usualy text-file. – Smamatti Oct 15 '11 at 10:03
  • so smamatti is there any other way of saving data into doc file – Sibu Oct 15 '11 at 10:08
  • Just save as `.html`. Word should be able to open that. – hakre Oct 15 '11 at 10:08
  • The problem is probably in the HTML. Please show the first few lines of what you are saving – Pekka Oct 15 '11 at 10:16
  • NO External Stylesheets should be used with the HTML solution – Dimitar Dimitrov Oct 15 '11 at 11:56

1 Answers1

1

There is no problem with your code. The problem is that the file you are creating is a regular text file with the doc extension, in other words not a real Word file. If you don't have to write a doc file, just keep it as a plain .txt and that will solve the problem.

Now if your project specs require you to have that file as a doc, you can do the following:

  1. Use the "HTML" approach(no COM required) Take a look at Sergey Kornilov's post: Create Word Document using PHP in Linux

    There is also a similar question here: Reading/Writing a MS Word file in PHP

  2. Use a COM Object - you will have to go that route if you need an elaborate word file

This is from my experience. Let's hope somebody will come up with a better and more efficient solution.

Good Luck!

UPDATE: I automatically assumed you are working in a Win environment. In this case COM will do, if you need it to work on a Linux machine, your alternative is OpenOffice

This is a decent article on COM and stuff: http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php#wordcom

For OpenOffice just look at their API - http://api.openoffice.org/ Take a look at their forum, I am sure they have examples with PHP.

My personal advice is to play with those, but work on a final solution after a day or two, if you have the time. Writing Word files is certainly not my forte, so there could possibly be another way of handling this.

Good luck!

UPDATE

<html>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<body>
<p>England would be keen to finish the summer on a high note by also remaining unbeaten in the upcoming ODI series against world champions India, said Test skipper Andrew Strauss after handing out the visitors a 4-0 whitewash.</p>
</body>
</html>
Community
  • 1
  • 1
Dimitar Dimitrov
  • 212
  • 1
  • 10
  • dimitar can you elaborate on creating word file using COM object – Sibu Oct 15 '11 at 10:34
  • Masking HTML code as a .doc file is not necessarily the problem, and can *sometimes* be a viable solution - Word will do its best to convert it. The problem *must* be with the HTML code that is being fed to Word and that the OP is still not showing. Still, your advice is sound and properly building a document is always the best way to go. – Pekka Oct 15 '11 at 10:38
  • How unfortunate that you have to go that route. I have not done that recently, let me look for a decent article and update my post. – Dimitar Dimitrov Oct 15 '11 at 10:38
  • If the headers are set correctly and HTML has open/close tags there is no reason for Word to not open the file, especially is it is a simple one. I know you probably checked this, but is there anything weird in the data you insert that is not escaped. – Dimitar Dimitrov Oct 15 '11 at 10:42
  • @Dimitar I think he probably did *not* check that :) – Pekka Oct 15 '11 at 10:43
  • @Sibu If this is all you are saving, I don't think there is any reason to go COM/OpenOffice, just mask it as HTML. This is the data but how are you creating the HTML? – Dimitar Dimitrov Oct 15 '11 at 11:17