如何将php中office文件转pdf文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
php office转pdf的方法:首先打开“php.ini”文件;然后开启dcom扩展;接着配置office组件服务;最后运行将office文件转化为pdf的代码文件即可。
将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, // wdExportFormatPDF
18 false, // open file after export
19 0, // wdExportOptimizeForPrint
20 3, // wdExportFromTo
21 1, // begin page
22 5000, // end page
23 7, // wdExportDocumentWithMarkup
24 true, // IncludeDocProps
25 true, // KeepIRM
26 1 // WdExportCreateBookmarks
27 );
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 }
关于如何将php中office文件转pdf文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。