【问题标题】:Convert Doc/Docx Files To PDF using PHP使用 PHP 将 Doc/Docx 文件转换为 PDF
【发布时间】:2012-12-24 08:14:45
【问题描述】:

我正在寻找一个 PHP 代码或 PHP 库来将 microsoft doc/docx 文件转换为 PDF。我对 Zend 不熟悉。请帮我成功地将我的word文件转换为PDF。

【问题讨论】:

  • 需要在线转换还是其他?
  • 我不需要在线转换器
  • 请输入拒绝投票的原因。这将帮助我避免错误。谢谢..

标签: php pdf docx


【解决方案1】:

打开办公室,这段代码对我有帮助:)

<?php
    set_time_limit(0);
    function MakePropertyValue($name,$value,$osm){
    $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
    $oStruct->Name = $name;
    $oStruct->Value = $value;
    return $oStruct;
    }
    function word2pdf($doc_url, $output_url){
    //echo $output_url;
    //Invoke the OpenOffice.org service manager
    $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
    //Set the application to remain hidden to avoid flashing the document onscreen
    $args = array(MakePropertyValue("Hidden",true,$osm));
    //Launch the desktop
    $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
    //Load the .doc file, and pass in the "Hidden" property from above
    $oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);
    //Set up the arguments for the PDF output
    $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
    //print_r($export_args);
    //Write out the PDF
    $oWriterDoc->storeToURL($output_url,$export_args);
    $oWriterDoc->close(true);
    }
    $output_dir = "C:/wamp/www/vineesh/";
    $doc_file = "C:/wamp/www/vineesh/vineesh.docx";
    $pdf_file = "vineesh.pdf";
    $output_file = $output_dir . $pdf_file;
    $doc_file = "file:///" . $doc_file;
    $output_file = "file:///" . $output_file;
    word2pdf($doc_file,$output_file);
    ?>

【讨论】:

  • 但我有错误此代码“致命错误:未捕获的异常 'com_exception' 并带有消息'无法创建 COM 对象 `com.sun.star.ServiceManager':C:\xampp\ 中的语法无效' htdocs\learn\word.php:11 堆栈跟踪:#0 C:\xampp\htdocs\learn\word.php(11): com->com('com.sun.star.Se...') #1 C:\xampp\htdocs\learn\word.php(30): word2pdf('file:///C:/xamp...', 'file:///C:/xamp...') #2 {main} 在第 11 行的 C:\xampp\htdocs\learn\word.php 中抛出
  • 请确保已安装 OpenOffice.org。在您的电脑或服务器上安装开放式办公室。
  • 如何在我的 xampp 服务器上安装 OpenOffice
  • 我猜这个解决方案只适用于windows系统?
  • 我猜COM只支持windows你可以看看这个php.net/manual/en/com.requirements.phpubuntu系统怎么样
【解决方案2】:

你是说 PHP 库吗?然后phpdocx 将为您工作。阅读有关安装和生成 pdf 的文档。

【讨论】:

    【解决方案3】:

    phpword 不会转换为 PDF,它只有助于创建 word 文档并在其上使用变量替换。

    【讨论】:

      【解决方案4】:

      如果您使用的是 Windows,COM package 是最佳选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 1970-01-01
        • 2016-11-20
        • 2015-09-13
        • 2021-04-23
        • 2018-01-05
        相关资源
        最近更新 更多