How do you display formatted Word Doc in HTML / PHP?

What is the best way to display formatted Word Doc in HTML / PHP?

Here is the code that I have, but it does not format it:

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("ACME.doc"));

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();
$word = null;
unset($word);
+3
source share
3 answers

I don't know anything about COM, but judging by the Word API docs on MSDN, it looks like your best bet would be to use Document.SaveAssave as wsFormatFilteredHTMLa temporary file and then pass that HTML code to the user. Remember to select filtered HTML, otherwise you will get soup-soup-soup sometime.

+3
source

I get it. Check out the solution for reading Word Doc and formatting it in HTML:

$filename = "ACME.doc";
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath($filename));

$new_filename = substr($filename,0,-4) . ".html";

// the '2' parameter specifies saving in txt format
// the '6' parameter specifies saving in rtf format
// the '8' parameter specifies saving in html format
$word->Documents[1]->SaveAs("C:/a1/projects/---full path--- /".$new_filename,8);
$word->Documents[1]->Close(false);
$word->Quit();
//$word->Release();
$word = NULL;
unset($word);

$fh = fopen($new_filename, 'r');
$contents = fread($fh, filesize($new_filename));
echo $contents;
fclose($fh);
//unlink($new_filename);

... "charset = UTF-8" PHP, ... .

, SaveAs , , , .

.

+4

All Articles