php 将office文件(word/excel/ppt)转化为pdf(windows和linux只要安装对应组件应该就行)

 

 

一、配置环境

(1)配置php.ini

添加:extension=php_com_dotnet.dll

com.allow_dcom = true  // 去掉号,改为true

重启环境

 

(2) 安装:WPS 专业版,或者 microsoft  office 2010

(microsoft office 2007 需要安装加载项:Microsoft Save as PDF) 

(3)配置office组件服务

        按 win+R 快捷键进入运行菜单,输入 Dcomcnfg 

        找到:     [组件服务] —— [计算机]—— [我的电脑] —— [DCOM配置] ——【wps……】或[Microsoft Wrord 97-2003文档]

        如果没找到【wps……】或(Microsoft Wrord 97-2003文档):

        按 win+R 快捷键进入运行菜单

        输入:mmc -32

        [文件]——[添加或删除管理单元]——[组件服务](从可用管理单元,添加到所选管理单元,点击:确定)

        添加完以后,在控制台根节点下,找到【wps……】或[Microsoft Wrord 97-2003文档],右键设置属性,设置“标识”为:交互式用户(还有安全里面的设置 可以看其他文章的设置)

 

 

注:我开始 选择交互式用户  : 会出现 我登录远程服务器一切正常,如果退出远程服务器实例化组件就报错,最后选择了 下列用户,填写了管理员用户和密码才正常可用.

 

二、编写程序

<?php

word2pdf();
 function word2pdf()
{
    $filenamedoc = dirname(__FILE__)."/index.docx";
    $filenamepdf = dirname(__FILE__)."/index.pdf";

    $dd = $word = new COM("KWPS.Application") or die ("Could not initialise Object.");
    // 或者 $dd = $word = new COM("Word.Application") or die ("Could not initialise Object.");
    // set it to 1 to see the MS Word window (the actual opening of the document)
    $word->Visible = 0;
    // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc"
    $word->DisplayAlerts = 0;
    // open the word 2007-2013 document

    $word->Documents->Open($filenamedoc);
    // save it as word 2003
    // convert word 2007-2013 to PDF

    //判断要生成的文件名是否存在
    if(file_exists($filenamepdf)) {
        //存在就删除
        unlink ($filenamepdf);
    }
    $word->ActiveDocument->ExportAsFixedFormat($filenamepdf, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
    // quit the Word process
    $word->Quit(false);
    // clean up
    unset($word);
    if(!function_exists('read_pdf')) {
        header('Content-type: application/pdf');
        header('filename='.$filenamepdf);
        readfile($filenamepdf);
        read_pdf('Python_study.pdf');
    }
    echo 'ok';
}

?>

 

if(!function_exists('read_pdf')) {
  function read_pdf($file) {
    if(strtolower(substr(strrchr($file,'.'),1)) != 'pdf') {
      echo '文件格式不对.';
      return;
    }
    if(!file_exists($file)) {
      echo '文件不存在';
      return;
    }
    header('Content-type: application/pdf');
    header('filename='.$file);
    readfile($file);
  }
}

 

 

 

 

 

转: https://my.oschina.net/u/3567851/blog/2909656

https://blog.csdn.net/sangjinchao/article/details/78053545

https://blog.csdn.net/ken2999/article/details/82353747  (组件的权限)

https://www.cnblogs.com/zhuchenglin/p/7586170.html

https://stackoverflow.com/questions/5538584/convert-word-doc-docx-and-excel-xls-xlsx-to-pdf-with-php

https://blog.csdn.net/baidu_27474941/article/details/83268468

 

转载于:https://www.cnblogs.com/fps2tao/p/11490140.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值