问题
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> </td>
</tr>
<tr>
<td style="text-align: left; height: 0.6cm">9AM Oct-12-08</td>
<td> </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