将office文件转化为pdf的方法有
1.利用openoffice提供的服务 (比较简单,但是转化的效果不太好)
2.使用office提供的服务 (注:这在windows 2019 服务器上,并且服务器上面安装了版本比较高的office 2013 版本呢)
下面重点介绍利用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组件服务
![图片[1]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/1654579287P1h65k457928h7.png)
![图片[2]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/16545792871x654G57928Yc7.png)
![图片[3]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/16545792871654K5z719X287.png)
![图片[4]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/1654579287n1G654579278W7.png)
![图片[5]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/1654579287165V45796C28J7.png)
![图片[6]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/1654579287165ye4579Ju287.png)
![图片[7]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/16545792871hXK6524579287.png)
![图片[8]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/165457928816Zf54d5792858.png)
![图片[9]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/1654579288165ZE4YX579288.png)
![图片[10]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/16545792881a6545798jv288.png)
像这样的操作还有两个!!
![图片[11]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/1654579288f1654579922A88.png)
![图片[12]-Windows 2019环境下 php 将office文件(word/excel/ppt)转化为pdf(转)-栗子博客](https://www.lizi.tw/wp-content/uploads/2022/11/16545792881yM65457892888.png)
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 }
© 版权声明
内容来源于网络或本站原创,如有任何问题请联系站长
THE END
暂无评论内容