- 准备工作:
- lumen 框架
- phpstudy
- php 7.4
- 需要 Fpdi 第三方扩展进行多个pdf 合并
- 下载Fpdi 组件:
- composer require setasign/fpdi,注意这个李composer一定要在llumen 的根目录所在的vender中目录中。这样才会下载到vender中
- 比较重要的是要将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;
}