在Linux环境下,PHP创建Word文档的可用解决方案有哪些?
PHPWord 能够生成 docx 格式的 Word 文档。它还可以使用现有的 .docx 文件作为模板,变量可以以 ${varname} 的格式添加到文档中。
它采用 LGPL 许可证,并且随附的代码示例对我来说很好用。
如果您需要生成“真实”的Word文档,您需要一个基于Windows的Web服务器和COM自动化。我强烈推荐Joel关于这个主题的文章。
一种相当常见(但不可靠)的替代方法是:
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
头并不能构成真正的文件格式转换。
OpenOffice模板 + OOo命令行接口。
有可用工具和库可简化每个步骤。
希望这可以帮助您。
在Linux平台上,使用PHP创建DOC文件的最简单方法就是使用Zend Framework组件phpLiveDocx。
以下来自该项目网站:
“phpLiveDocx允许开发人员通过将PHP中的结构化数据与在文字处理器中创建的模板相结合,生成文档。生成的文档可以保存为PDF、DOCX、DOC或RTF文件。这个概念与邮件合并的概念相同。”
OpenTBS 可以使用模板技术在 PHP 中创建 DOCX 动态文档。
无需临时文件,无需命令行,全部在 PHP 中实现。
它可以添加或删除图片。所创建的文档可以作为 HTML 下载、保存在服务器上的文件或作为 PHP 中的二进制内容生成。
它还可以合并 OpenDocument 文件(ODT、ODS、ODF 等)。
接着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 Name],用Mooo替换[Person Last Name]:
$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);
}
请注意,如果要替换的字符串过于普遍,此函数可能会破坏文档。尝试使用详细的替换字符串,例如[人名]。
Apache项目有一个名为POI的库,可用于生成MS Office文件。它是一个Java库,但优点是可以在Linux上无障碍运行。该库有其限制,但可能适合您的工作,并且使用起来比尝试运行Word要简单。
另一个选择是OpenOffice,但我不能确切地推荐它,因为我从未使用过。
<?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);
?>
创建高质量的Word文档有两种选择。一种是使用COM与Word进行通信(这至少需要一个Windows PHP服务器)。另一种是使用OpenOffice及其API来创建并保存Word格式的文档。