php word/excel/ppt 转pdf

转载至:https://blog.csdn.net/sangjinchao/article/details/78053545

把代码放到了github上,点击进入

前阶段有个项目用到了线上预览功能,

关于预览office文件实现核心就是,把office文件(word,excel,ppt)转为pdf文件,

然后再把pdf文件经过pdf2html转为html文件进行预览。

利用openoffice的话,非常简单,不过效果不太好,转成的pdf样式会乱!

这里就不做介绍了。

下面我把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.php代码:

ppt转pdf代码

public function ppt_to_pdf() {
$srcfilename = ‘E:/aa.ppt’;
KaTeX parse error: Expected '}', got 'EOF' at end of input: …f(!file_exists(srcfilename)){
return;
}
$ppt = new \COM(“powerpoint.application”) or die(“Unable to instantiate Powerpoint”);
$presentation = p p t − > P r e s e n t a t i o n s − > O p e n ( ppt->Presentations->Open( ppt>Presentations>Open(srcfilename, false, false, false);
p r e s e n t a t i o n − > S a v e A s ( presentation->SaveAs( presentation>SaveAs(destfilename,32,1);
$presentation->Close();
$ppt->Quit();
} catch (\Exception KaTeX parse error: Expected '}', got 'EOF' at end of input: …(method_exists(ppt, “Quit”)){
$ppt->Quit();
}
return;
}
}

excel转ppt

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;
    }
}

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;
    }
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值