Windows服务器OFFICE 转PDF (Lumen) 二(代码篇)

  1. 准备工作:
    1. lumen 框架 
    2. phpstudy
    3. php 7.4  
    4. 需要 Fpdi  第三方扩展进行多个pdf 合并
  2. 下载Fpdi 组件:
    1. composer require setasign/fpdi,注意这个李composer一定要在llumen 的根目录所在的vender中目录中。这样才会下载到vender中
    2. 比较重要的是要将vender中的  pdfi 安装到LUMEN中,,这也是安装第三方组件的重要一步。 步骤如下图

                

 然后执行  composer install 。这样就会将autoload 中的配置安装到lumen中,我们就能在框架中使用这些组件了

3. 编写office转换代码: 

//通过windows的COM组件将ppt 转成 pdf
if(!function_exists('ppt2Pdf')){

    function ppt2Pdf($srcfilename,$destfilename){

        $responsInfo = array(
            "status"=>"success",
            "errorMsg"=>""
        );
        
      
        try {
            $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 (\Throwable $e) {
                
                $responsInfo = array(
                    "status"=>"failed",
                    "errorMsg"=>"src:$srcfilename catch exception:" . $e->getMessage()
                );
                if (method_exists($ppt, "Quit")){
                    $ppt->Quit();
                }
               
            }

        return $responsInfo;
    }
}
//通过windows的COM组件将excel 转成 pdf
if(!function_exists('excel2Pdf')){
    function excel2Pdf($srcfilename,$destfilename){

        $responsInfo = array(
            "status"=>"success",
            "errorMsg"=>""
        );

        try {
            $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 (\Throwable $e) {
    
            $responsInfo = array(
                "status"=>"failed",
                "errorMsg"=>"src:$srcfilename catch exception:" . $e->getMessage()
            );
    
            if (method_exists($excel, "Quit")){
    
                $excel->Quit();
    
            }
        }

        return $responsInfo;
    }

}

if(!function_exists('word2Pdf')){
    function word2Pdf($srcfilename,$destfilename){

        $responsInfo = array(
            "status"=>"success",
            "errorMsg"=>""
        );

        try {

            $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 (\Throwable $e) {

            $responsInfo = array(
                "status"=>"failed",
                "errorMsg"=>"src:$srcfilename catch exception:" . $e->getMessage()
            );

            if (method_exists($word, "Quit")){

                $word->Quit();

            }
        }
        return $responsInfo;
    }
}
//合并PDF
public function mergePdf()
    {
        try{
            $pdf = new Fpdi();
            //获得数据来源,这里换成可以做修改换成你想要的文件
            $files = File::allFiles($this->converedFilePath);
            if(!empty($files)){

                foreach ($files as $file) {
                    $pageCount = $pdf->setSourceFile($file->getPathname());
                    for ($i=1;$i<=$pageCount;$i++) {
                        $pdf->AddPage();
                        $tplIdx = $pdf->importPage($i);
                        $size = $pdf->getTemplateSize($tplIdx);
                    //这里根据宽度和长度。设置PDF横过来还是竖过来
                        $pdf->setPageFormat(array($size['width'],$size['height']),$size['orientation']);
                        $pdf->useTemplate($tplIdx);
                    
                    };
                    
                    PlmOfficeFile::updateFileMergeStatusByFileNameAndFormId($this->plmFomfId,$file->getFilename());
                }
                $mergePdfNme = $this->plmFomfId.".pdf";
                $pdf->Output("F", $this->mergedFilePath."/".$mergePdfNme);
                $pdf->close();

            }else{
                throw new Exception("目录为空");
            }
            
        }catch(\Throwable $e){
            Log::info("mergePdf: ".$e->getMessage());
            return false;
        }

        return true;
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值