How to View a Word Document From PHP (4 Steps)

By Jim Campbell

Display Word document from PHP.
i computer image by blaine stiger from Fotolia.com

Word documents are rich-text documents that allow you to create newsletters, contracts and other customer communication. You can display these Word documents in the user's browser using the PHP programming language. The PHP programming language provides developers with a free resource to create dynamic webpages. You can create dynamic content by displaying a Word document for the web reader.

Step 1

Create your Word application and file name variable. These variables are required to point PHP to the file and use the internal Word functions. The following code creates the variables:

$app= new COM("Word.Application"); $file = "/worddoc.doc";

Step 2

Open the Word document and set its attribute to visible. The following code opens the Word document:

$app->visible = true; $app->Documents->Open($file);

Step 3

Print out the Word document to the web browser. The following code prints the Word document to the browser, so your readers can view the file:

$app->ActiveDocument->PrintOut();

Step 4

Close the document and quit the application. The Word document still displays in the user's browser, but the memory resources are freed on the server. The following code closes your document:

$app->ActiveDocument->Close(); $app->Quit();

×