我正在尝试在 Windows 服务器上使用 PHP 将 .docx 文件转换为 pdf。我尝试了其他帖子中的几个解决方案,包括 phpdocx (它的转换效果非常差,不保留任何格式)和 php 的 Com 对象。我只有 Office 2003,所以没有使用 Com 的 pdf 转换器。
我想过使用 OpenOffice/LibreOffice,但没有找到任何有关在 Windows 服务器上安装和使用 Com 的信息(我知道可以安装它,但我不知道如何为这些设置 Com)它)。
由于表单上的数据(它们必须保留在我们的服务器上),因此不能选择使用网络服务。这意味着Zend Framework无法使用。
任何建议或有关将 Com 与 Open Office 结合使用的信息都会有所帮助。
最佳答案
我终于能够让这个工作了。我们的问题是Word 2003 中没有PDF 转换器。我们最终使用了 Office 2010 的试用版(假设一切正常,我们将购买完整版)。 Word 2007 也可以。下面是我用来实现此功能的代码:
//Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application') or die('MS Word could not be loaded');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0;
$word->DisplayAlerts = 0;
try{
$doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "doc opened";
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "created pdf";
$word->Quit();
$word = null;
关于php - 在 Windows Server 上将 Word Doc 文件转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902720/