php Word成功轉PDF(wps/microsoft word)

1、配置環境php

(1)配置php.iniapp

添加:extension=php_com_dotnet.dllui

com.allow_dcom = true  // 去掉號,改成truespa

重啓環境code

(2) 安裝:WPS 專業版,或者 microsoft  office 2010orm

(microsoft office 2007 須要安裝加載項:Microsoft Save as PDF https://www.microsoft.com/zh-CN/download/details.aspx?id=7 ) blog

(3)配置office組件服務文檔

        按 win+R 快捷鍵進入運行菜單,輸入 Dcomcnfg get

        找到:     [組件服務] —— [計算機]—— [個人電腦] —— [DCOM配置] ——【wps……】或[Microsoft Wrord 97-2003文檔]it

        若是沒找到【wps……】或(Microsoft Wrord 97-2003文檔):

        按 win+R 快捷鍵進入運行菜單

        輸入:mmc -32

        [文件]——[添加或刪除管理單元]——[組件服務](從可用管理單元,添加到所選管理單元,點擊:肯定)

        添加完之後,在控制檯根節點下,找到【wps……】或[Microsoft Wrord 97-2003文檔],右鍵設置屬性,設置「標識」爲:交互式用戶

        

        

2、編寫程序

<?php

word2pdf();
 function word2pdf()
{
    $filenamedoc = dirname(__FILE__)."/index.docx";
    $filenamepdf = dirname(__FILE__)."/index.pdf";

    $dd = $word = new COM("KWPS.Application") or die ("Could not initialise Object.");
    // 或者 $dd = $word = new COM("Word.Application") or die ("Could not initialise Object.");
    // set it to 1 to see the MS Word window (the actual opening of the document)
    $word->Visible = 0;
    // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc"
    $word->DisplayAlerts = 0;
    // open the word 2007-2013 document

    $word->Documents->Open($filenamedoc);
    // save it as word 2003
    // convert word 2007-2013 to PDF

    //判斷要生成的文件名是否存在
    if(file_exists($filenamepdf)) {
        //存在就刪除
        unlink ($filenamepdf);
    }
    $word->ActiveDocument->ExportAsFixedFormat($filenamepdf, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
    // quit the Word process
    $word->Quit(false);
    // clean up
    unset($word);
    if(!function_exists('read_pdf')) {
        header('Content-type: application/pdf');
        header('filename='.$filenamepdf);
        readfile($filenamepdf);
        read_pdf('Python_study.pdf');
    }
    echo 'ok';
}

?>

相關文章
相關標籤/搜索