2012-10-15 121 views
2

我正尝试使用Windows服务器上的PHP将.docx文件转换为pdf。我尝试了其他帖子的几个解决方案,其中包括phpdocx(它的转换非常糟糕,不会保留任何格式)和php的Com对象。我只有Office 2003,所以没有使用Com的PDF转换器。在Windows服务器上将Word文档文件转换为PDF

我想过使用OpenOffice/LibreOffice,但没有找到任何有关在Windows服务器上安装和使用Com的信息(我知道它可以安装,但我无法弄清楚如何设置Com为了它)。

由于表单上的数据(它们必须保留在我们的服务器上),所以不能使用web服务。这意味着Zend Framework不能使用。

任何建议将有所帮助,或有关使用COM与Open Office的信息。

+0

几个月前我使用PowerShell做了同样的事情。它基本上是一个批处理脚本,可以将某人保存为PDF。可能值得研究:) – BIOSTALL

+0

是否有你想使用Com的原因? –

+0

@Jowierun我使用Com,因为我不允许在他们的系统中引入新的脚本语言(比如python)。如果没有某种命令行工具,这是我知道的与PHP之外的应用程序交互的唯一方式。 – Mike

回答

2

我终于能够得到这个工作。 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;