在Linux中使用PHP创建Word文档

时间:2020-03-06 14:38:16  来源:igfitidea点击:

PHP在Linux环境中创建Word文档的可用解决方案是什么?

解决方案

真实的Word文档

如果需要生成"真实的" Word文档,则需要基于Windows的Web服务器和COM自动化。我强烈推荐乔尔(Joel)在该主题上的文章。

伪造的HTTP标头,用于欺骗Word打开原始HTML

较常见(但不可靠)的替代方法是:

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

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b>Fake word document</b>";
echo "</body>";
echo "</html>"

确保我们不使用外部样式表。一切都应该在同一个文件中。

请注意,这不会发送实际的Word文档。它只是欺骗浏览器提供下载和默认为.doc文件扩展名。较旧版本的Word可能经常在没有任何警告/安全消息的情况下打开它,而只是将原始HTML导入到Word中。 PHP发送带有误导性的" Content-Type"标头并不构成真正的文件格式转换。

有2个选项可以创建高质量的Word文档。使用COM与Word通信(至少需要Windows php服务器)。使用openoffice及其API以Word格式创建和保存文档。

Apache项目有一个称为POI的库,可用于生成MS Office文件。它是一个Java库,但优点是它可以在Linux上毫无问题地运行。该库有其局限性,但它可以为我们完成工作,并且它可能比尝试运行Word更容易使用。

另一个选择是OpenOffice,但由于我从未使用过,所以我不能完全推荐它。

OpenOffice模板+ OOo命令行界面。

  • 手动创建带有占位符的ODT模板,例如[%value-to-replace%]
  • 在PHP中使用实际数据实例化模板时,请解压缩模板ODT(它是压缩的XML),然后针对XML运行以实际值替换占位符的文本。
  • 向后压缩ODT
  • 通过OpenOffice命令行界面运行转换ODT-> DOC。

有一些工具和库可简化每个步骤。

可能有帮助。

到目前为止,使用PHP在Linux上创建DOC文件的最简单方法是使用Zend Framework组件phpLiveDocx。

从项目网站:

" phpLiveDocx允许开发人员通过将来自PHP的结构化数据与在字处理器中创建的模板进行组合来生成文档。生成的文档可以另存为PDF,DOCX,DOC或者RTF文件。其概念与邮件合并相同。"

<?php
function fWriteFile($sFileName,$sFileContent="No Data",$ROOT)
    {
        $word = new COM("word.application") or die("Unable to instantiate Word");
        //bring it to front
        $word->Visible = 1;
        //open an empty document
        $word->Documents->Add();
        //do some weird stuff
        $word->Selection->TypeText($sFileContent);
        $word->Documents[1]->SaveAs($ROOT."/".$sFileName.".doc");
        //closing word
        $word->Quit();
        //free the object
        $word = null;
        return $sFileName;
    }
?>

<?php
$PATH_ROOT=dirname(__FILE__);
$Return ="<table>";
$Return .="<tr><td>Row[0]</td></tr>";
 $Return .="<tr><td>Row[1]</td></tr>";
$sReturn .="</table>";
fWriteFile("test",$Return,$PATH_ROOT);
?>

紧随Ivan Krechetov的回答,下面是一个函数,该函数不需要额外的库就可以对docx和odt进行邮件合并(实际上只是简单的文本替换)。

function mailMerge($templateFile, $newFile, $row)
{
  if (!copy($templateFile, $newFile))  // make a duplicate so we dont overwrite the template
    return false; // could not duplicate template
  $zip = new ZipArchive();
  if ($zip->open($newFile, ZIPARCHIVE::CHECKCONS) !== TRUE)
    return false; // probably not a docx file
  $file = substr($templateFile, -4) == '.odt' ? 'content.xml' : 'word/document.xml';
  $data = $zip->getFromName($file);
  foreach ($row as $key => $value)
    $data = str_replace($key, $value, $data);
  $zip->deleteName($file);
  $zip->addFromString($file, $data);
  $zip->close();
  return true;
}

这将取代
[个人名称]和Mina
和[Person Last Name]和Mooo:

$replacements = array('[Person Name]' => 'Mina', '[Person Last Name]' => 'Mooo');
$newFile = tempnam_sfx(sys_get_temp_dir(), '.dat');
$templateName = 'personinfo.docx';
if (mailMerge($templateName, $newFile, $replacements))
{
  header('Content-type: application/msword');
  header('Content-Disposition: attachment; filename=' . $templateName);
  header('Accept-Ranges: bytes');
  header('Content-Length: '. filesize($file));
  readfile($newFile);
  unlink($newFile);
}

请注意,如果要替换的字符串过于笼统,则此函数可能会损坏文档。尝试使用冗长的替换字符串,例如[Person Name]。

看看PHP COM文档(这些注释很有用)http://us3.php.net/com