将office文件转化为pdf的方法有
1.利用openoffice提供的服务 (比较简单,但是转化的效果不太好)
2.使用office提供的服务 (注:这在windows服务器上,并且服务器上面安装了版本比较高的office)
下面重点介绍利用office服务将office文件转化为pdf
1.php开启dcom扩展
打开php.ini,搜索php_com_dotnet和php_com_dotnet:
extension=php_com_dotnet.dll //把前面的分号去掉
com.allow_dcom = true //改为true
重启apache
2.配置office组件服务
.
像这样的操作还有两个!!
3.下面就该介绍将office文件转化为pdf的代码了
(1)ppt转pdf代码
1 public function ppt_to_pdf() { 2 $srcfilename = 'E:/aa.ppt'; 3 $destfilename = 'E:/aa.pdf'; 4 try { 5 if(!file_exists($srcfilename)){ 6 return; 7 } 8 $ppt = new \COM("powerpoint.application") or die("Unable to instantiate Powerpoint"); 9 $presentation = $ppt->Presentations->Open($srcfilename, false, false, false);10 $presentation->SaveAs($destfilename,32,1);11 $presentation->Close();12 $ppt->Quit();13 } catch (\Exception $e) {14 if (method_exists($ppt, "Quit")){15 $ppt->Quit();16 }17 return;18 }19 }
(2)excel转pdf代码
1 public function excel_to_pdf() { 2 $srcfilename = 'E:/aa.xls'; 3 $destfilename = 'E:/aa.pdf'; 4 try { 5 if(!file_exists($srcfilename)){ 6 return; 7 } 8 $excel = new \COM("excel.application") or die("Unable to instantiate excel"); 9 $workbook = $excel->Workbooks->Open($srcfilename, null, false, null, "1", "1", true);10 $workbook->ExportAsFixedFormat(0, $destfilename);11 $workbook->Close();12 $excel->Quit();13 } catch (\Exception $e) {14 echo ("src:$srcfilename catch exception:" . $e->__toString());15 if (method_exists($excel, "Quit")){16 $excel->Quit();17 }18 return;19 }20 }
(3)word转pdf代码(其他的文本格式的文件也可以使用这个,例:txt文件)
1 public function doc_to_pdf() { 2 $srcfilename = 'E:/aa.doc'; 3 $destfilename = 'E:/aa.pdf'; 4 try { 5 if(!file_exists($srcfilename)){ 6 return; 7 } 8 9 $word = new \COM("word.application") or die("Can't start Word!");10 $word->Visible=0;11 $word->Documents->Open($srcfilename, false, false, false, "1", "1", true);12 13 $word->ActiveDocument->final = false;14 $word->ActiveDocument->Saved = true;15 $word->ActiveDocument->ExportAsFixedFormat(16 $destfilename,17 17, // wdExportFormatPDF18 false, // open file after export19 0, // wdExportOptimizeForPrint20 3, // wdExportFromTo21 1, // begin page22 5000, // end page23 7, // wdExportDocumentWithMarkup24 true, // IncludeDocProps25 true, // KeepIRM26 1 // WdExportCreateBookmarks27 );28 $word->ActiveDocument->Close();29 $word->Quit();30 } catch (\Exception $e) {31 if (method_exists($word, "Quit")){32 $word->Quit();33 }34 return;35 }36 }
注:本文参考地址: