我终于能够得到这个工作。 OUr问题在于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;
几个月前我使用PowerShell做了同样的事情。它基本上是一个批处理脚本,可以将某人保存为PDF。可能值得研究:) – BIOSTALL
是否有你想使用Com的原因? –
@Jowierun我使用Com,因为我不允许在他们的系统中引入新的脚本语言(比如python)。如果没有某种命令行工具,这是我知道的与PHP之外的应用程序交互的唯一方式。 – Mike