php使用COM组件 office文件(word/excel/ppt)转pdf文件

2020-05-30

php7.0 - office2013  office_to_pdf

php开启dcom扩展

打开php.ini,搜索php_com_dotnet和php_com_dotnet:

extension=php_com_dotnet.dll   //把前面的分号去掉
com.allow_dcom = true  //改为true

重启服务

配置office组件服务

支持word

 

如果这种方式找不到可以使用另一种方式

其他步骤上面相同

支持excel和ppt

使用php操作文档生成pdf

/**
* @description: word转pdf
* @param srcfilename  要转换的word的全路径 E:/aa.doc
* @param destfilename 转换后的pdf的全路径   E:/aa.pdf
* @return: 
*/  
function doc_to_pdf($srcfilename,$destfilename) {
    try {
         if(!file_exists($srcfilename))return;
         $word = new \COM("word.application") or die("Can't start Word!");
         $word->Visible=0;
         $word->Documents->Open($srcfilename, false, false, false, "1", "1", true);
           
         $word->ActiveDocument->final = false;
         $word->ActiveDocument->Saved = true;
         $word->ActiveDocument->ExportAsFixedFormat(
             $destfilename,
             17,                         // wdExportFormatPDF
             false,                      // open file after export
             0,                          // wdExportOptimizeForPrint
             3,                          // wdExportFromTo
             1,                          // begin page
             5000,                       // end page
             7,                          // wdExportDocumentWithMarkup
             true,                       // IncludeDocProps
             true,                       // KeepIRM
             1                           // WdExportCreateBookmarks
         );
         $word->ActiveDocument->Close();
         $word->Quit();
     } catch (\Exception $e) {
         if (method_exists($word, "Quit"))$word->Quit();
         return;
     }
}
/**
 * @description: ppt转pdf
 * @param srcfilename  要转换的ppt的全路径 E:/aa.doc E:/aa.ppt
 * @param destfilename 转换后的pdf的全路径 E:/aa.pdf
 * @return: 
 */
function ppt_to_pdf($srcfilename,$destfilename) {
    try {
        if(!file_exists($srcfilename)){
            return;
        }
        $ppt = new \COM("powerpoint.application") or die("Unable to instantiate Powerpoint");
        $presentation = $ppt->Presentations->Open($srcfilename, false, false, false);
        $presentation->SaveAs($destfilename,32,1);
        $presentation->Close();
        $ppt->Quit();
    } catch (\Exception $e) {
        if (method_exists($ppt, "Quit")){
            $ppt->Quit();
        }
        return;
    }
}
/**
 * @description: excel转pdf
 * @param srcfilename  要转换的excel的全路径 E:/aa.doc E:/aa.xls
 * @param destfilename 转换后的pdf的全路径 E:/aa.pdf
 * @return: 
 */
function excel_to_pdf($srcfilename,$destfilename) {
    try {
        if(!file_exists($srcfilename)){
            return;
        }
        $excel = new \COM("excel.application") or die("Unable to instantiate excel");
        $workbook = $excel->Workbooks->Open($srcfilename, null, false, null, "1", "1", true);
        $workbook->ExportAsFixedFormat(0, $destfilename);
        $workbook->Close();
        $excel->Quit();
    } catch (\Exception $e) {
            echo ("src:$srcfilename catch exception:" . $e->__toString());
        if (method_exists($excel, "Quit")){
            $excel->Quit();
        }
        return;
    }
}

如果代码执行报错,将office设置为默认

调用com时的错误

1、
ERR: Failed to create COM object `word.Application': 尚未调用 CoInitialize。
查看你的winword.exe进程是不是非常多,全部关就好了。因为com调用失败时,winword.exe不会自动关闭
2、
ERR: <b>Source:</b> Microsoft Word<br/><b>Description:</b> 因为没有打开的文档,所以这一命令无效。
使用命令行执行,看是否能够执行成功。大概率是因为php没有调用office的权限

文章来源于:https://blog.csdn.net/sangjinchao/article/details/78053545

{/if}