This HTML Word document displays incorrectly in OpenOffice.org

梦想与她 提交于 2019-12-11 05:28:45

问题


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.


回答1:


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]-->



回答2:


I think the easiest way to generate DOC files with PHP is using the Zend Framework component phpLiveDocx. You can load Word or Open Office templates, merge textual data and save the final document to a number of formats, such as DOC, DOCX, RTF and PDF.

Learn more on the project web site:

http://www.phplivedocx.org/articles/brief-introduction-to-phplivedocx/



来源:https://stackoverflow.com/questions/688524/this-html-word-document-displays-incorrectly-in-openoffice-org

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!