将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组件服务
在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Microsoft Excel程序,这主要是64位系统的问题,excel是32位的组件,所以在正常的系统组件服务里是看不到的
可以通过在运行里面输入 comexp.msc -32 来打开32位的组件服务,在里就能看到excel组件了,希望对您有所帮助
.
像这样的操作还有两个!!
3.下面就该介绍将office文件转化为pdf的代码了
(1)ppt转pdf代码
public function ppt_to_pdf() { $srcfilename = \'E:/aa.ppt\'; $destfilename = \'E:/aa.pdf\'; 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; } }
(2)excel转pdf代码
public function excel_to_pdf() { $srcfilename = \'E:/aa.xls\'; $destfilename = \'E:/aa.pdf\'; 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; } }
(3)word转pdf代码(其他的文本格式的文件也可以使用这个,例:txt文件)
public function doc_to_pdf() { $srcfilename = \'E:/aa.doc\'; $destfilename = \'E:/aa.pdf\'; 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; } }