php操作openoffice把文件轉換成pdf


  1.上官網, http://www.openoffice.org。下載openoffice4.1.2

‍  2.設置權限
    cmd 運行Dcomcnfg.exe->組件服務->計算機->我的電腦->DCOM配置->OpenOffice Service Manager

  3.打開openoffice   在進程中查看打開此進程的用戶是誰  然後設置此用戶的訪問權限爲允許

圖片.png



然後將標識調整爲交互式

圖片.png


4.在php環境下運行此代碼

<?php
/**
 * Author: 宇翔大魔王
 * Date: 17-1-6
 * Time: 下午2:25
 * TOpdf
 */
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){
    //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
        $top = $osm->createInstance("com.sun.star.frame.Desktop");
    //Load the .doc file, and pass in the "Hidden" property from above
    $oWriterDoc = $top->loadComponentFromURL($doc_url,"_blank", 0, $args);
    //Set up the arguments for the PDF output
    $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
    //Write out the PDF
    $oWriterDoc->storeToURL($output_url,$export_args);
    $oWriterDoc->close(true);
}
$doc_file = "file:///D:/wamp/www/study/phpr/201701/11.doc";
$output_file = 'file:///D:/wamp/www/study/phpr/201701/11.pdf';
word2pdf($doc_file,$output_file);

?>

注意: 易錯點:1.php首先得開啓com組件

                              php5.45之前在php.ini設置com.allow_dcom = true

                              php5.45之後extension=php_com_dotnet.dll

                          2.doc_file和output_file路徑必須嚴格要求爲

                                file:///D:/wamp/www/study/phpr/201701/11.pdf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章