Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Word document in read mode using php?

Tags:

php

I want to read a Word document in a browser using PHP. I have been trying for more than two hours, but I cant get it to work. Can someone help me do this?

like image 564
Ramamoorthy Avatar asked Jan 21 '26 17:01

Ramamoorthy


1 Answers

As your not too detailed in your question on your specific requirements or server setup, this will get you started & also answers the question.

<?php
$DocumentPath="C:/xampp/htdocs/test.doc";
//Create an instance of the Word application
$word = new COM("word.application") or die("Unable to instantiate application object");
//Creating an instance of the Word Document object
$wordDocument = new COM("word.document") or die("Unable to instantiate document object");
$word->Visible = 0;
//Open up an empty document
$wordDocument = $word->Documents->Open($DocumentPath);

//Create the filename for the HTML version
$HTMLPath = substr_replace($DocumentPath, 'html', -3, 3);
//Save the document as HTML
$wordDocument->SaveAs($HTMLPath, 3);
// clean up
$wordDocument = null;
$word->Quit();
$word = null;
// read the newly-created document
readfile($HTMLPath);
//delete the file
unlink($HTMLPath)
?>
like image 150
Lawrence Cherone Avatar answered Jan 23 '26 07:01

Lawrence Cherone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!