Php – This HTML Word document displays incorrectly in OpenOffice.org

ms-wordopenoffice.orgPHP

I have this simple code in php:

<?php

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=kid_tag.doc");

echo '<table cellspacing="0" cellpadding="0" border="0" width="8.4cm">
    <tr>
    <td colspan="3" style="text-align: right; height: 0.6cm">Nursery</td>
    </tr>
    <tr>
        <td style="height: 1.8cm"><img src="http://images.funadvice.com/photo/image/old/6943/tiny/cat.jpg" /></td>
        <td style="text-align: center; font-weight: bold">Sofia Abello</td>
    <td>&nbsp;</td>
    </tr> 
    <tr>
    <td style="text-align: left; height: 0.6cm">9AM Oct-12-08</td>
    <td>&nbsp;</td>
    <td style="text-align: right">Dance Studio</td>
    </tr>  
</table>';

?>

displays ok with MS Office Word, however, width is reduced (not proper width!) when opened with open office writer.

Best Answer

You are actually importing HTML into MS Word and OpenOffice.org. HTML is not the native format of neither Word nor OpenOffice.org, which means the input has to be converted first.

It is no surprise that these applications (whose main purpose is the editing of documents in the application's native format) are doing not a perfect job there. In fact - and that's not a big secret - not even web browsers, whose main purpose is the rendering of HTML are not perfect in that area.

The solution would be to provide HTML which works in both applications. You could do that using conditional comments which are a proprietary Microsoft extension to HTML and therefore only understood by Microsoft products.

This is how it could look like in your example:

<![if !mso]>
<table cellspacing="0" cellpadding="0" border="0" width="8.4cm">
  <tr>
    <td>OpenOffice.org Version</td>
  </tr>
</table>
<![endif]>
<!--[if mso]>
<table cellspacing="0" cellpadding="0" border="0" width="8.4cm">
  <tr>
    <td>Microsoft Word version</td>
  </tr>
</table>
<![endif]-->
Related Topic