Displaying a Microsoft Word Document in a web page.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Displaying a Microsoft Word Document in a web page.

Post by JackD »

I can get the Microsoft Word document to display on our web page, but it does not display cleanly and has all the formatting characters scattered across the screen. Is there a way to filter the document so the formating is applied and the document appears cleanly? If not, is there a way to do that with a PDF file? Here is the code we are using:

Code: Select all

<html>
<head>
<title>Open Document</title>
</head>
<body>
<DIV align="CENTER">
<?php
$Text='';
$Article=trim($_GET['file']);
if($Article != '') 
  { $Text=file_get_contents($Article);
    echo $Text;    
  } 
echo '<table BORDER="1" CELLSPACING="1" CELLPADDING="1" WIDTH="100%"><tr>';
echo '<TD WIDTH="10%" ALIGN="left" VALIGN="TOP">';
echo '<a href="doc.php?file=C:\myfiles\Cancer Part 2.doc" target=docwindow>Cancer Part 2</A><br>';
echo '<a href="doc.php?file=C:\myfiles\Cancer Part 2.doc" target=docwindow>Cancer Part 2</A><br>';
echo '<a href="doc.php?file=C:\myfiles\Cancer Part 3.doc" target=docwindow>Cancer Part 3</A>';
echo '<TD WIDTH="75%" ALIGN="CENTER" VALIGN="TOP">';
echo '<iframe name="docwindow" frameborder=6 width=800 height=600></iframe>';
echo '</CENTER></TD></TR></TABLE>';

?>
</DIV>
</body>
</html>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Displaying a Microsoft Word Document in a web page.

Post by requinix »

If they're actual .doc files and not HTML with the wrong extension then you need to display the file contents and only the file contents. No extra HTML.

Code: Select all

<?php

if (the script should show a file) {
    header("Content-Type: application/msword");
    show file contents here
    exit;
}

show the HTML to let the user choose a file

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Displaying a Microsoft Word Document in a web page.

Post by John Cartwright »

tasairis wrote:If they're actual .doc files and not HTML with the wrong extension then you need to display the file contents and only the file contents. No extra HTML.

Code: Select all

<?php

if (the script should show a file) {
    header("Content-Type: application/msword");
    show file contents here
    exit;
}

show the HTML to let the user choose a file

?>
Which would only work if the enduser has Microsoft Word installed on his computer. If you want a universal solution, your best bet would be to convert your word documents to HTML format (Word can do this) and provide the original document for download.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Displaying a Microsoft Word Document in a web page.

Post by JackD »

The documents are actually Word .doc documents. Somewhere I had read that IE and Firefox knew how to correctly display such documents. I was just looking for the mechanism. If I have to convert them all of the time, why use Word? The browsers gripe when there are two consecutive blanks, periods, etc. in a sentence, or when you use an apostrope or quotes.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Displaying a Microsoft Word Document in a web page.

Post by requinix »

JackD wrote:Somewhere I had read that IE and Firefox knew how to correctly display such documents.
They only know if, as John said, the user has Word (or an appropriate viewer) installed on their machine. No browser comes with built-in support for it.
Shellyyin
Forum Newbie
Posts: 8
Joined: Thu Sep 10, 2015 1:49 am

Re: Displaying a Microsoft Word Document in a web page.

Post by Shellyyin »

How about using a plugin? You can try Aceoffix. It can call the local MS Word to open in the web browsers.
Post Reply