【问题标题】:Word Doc File to PDF on Windows ServerWindows Server 上的 Word Doc 文件到 PDF
【发布时间】:2012-10-15 19:33:38
【问题描述】:

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

我想过使用 OpenOffice/LibreOffice,但没有找到任何关于在 windows 服务器上安装和使用 Com 的信息(我知道可以安装,但我不知道如何设置 Com它)。

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

任何建议都会有所帮助,或者有关将 Com 与 Open Office 结合使用的信息。

【问题讨论】:

  • 几个月前我使用 PowerShell 做了同样的事情。它本质上是一个模拟某人保存为 PDF 的批处理脚本。可能值得研究:)
  • 您是否有理由要使用 Com?
  • @Jowierun 我使用 Com 是因为我不允许在他们的系统中引入新的脚本语言(例如 python)。这是我所知道的在没有某种命令行工具的情况下与 PHP 之外的应用程序交互的唯一方法。

标签: php pdf windows-server-2008 docx


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2016-11-20
    • 2012-10-13
    相关资源
    最近更新 更多