public function createPdf($pdfReportList, $data,$salaryReport)
{
$corpId = $data['corpId'];
$monthly = $data['monthly'];
$pdf = new TCPDF();
$pdf->SetTitle('salaryrep');
// 去掉页眉
$pdf->SetPrintHeader(false);
// 去掉页脚
$pdf->SetPrintFooter(false);
// set font
$pdf->SetFont(Constant::$PDF_FONT, '', 9);
// 设置页面边距为零
$pdf->SetMargins(1, 3, 3, true);
// 关闭自动分页功能
$pdf->SetAutoPageBreak(true);
$pdf->SetPageOrientation('L');
// add a page
$pdf->AddPage();
//查询企业名称
$corpInfo = $this->corporationRepository->find($corpId);
//第一页截取6个
$pageOneList = [];
if(!empty($pdfReportList)){
$pageOneList = array_slice($pdfReportList, 0,6);;
}
$pdfData = [
'list' => $pageOneList,
'title' => $corpInfo->getCorpName(),
'monthly' => $data['monthlyFmt'],
'printDate' => date(Constant::$YMD_TEXT),
];
// 加载自定义 HTML 模板
$html = $this->twig->render('othersalary/pdf.html.twig',$pdfData);
$pdf->writeHTML($html, true, false, true);
$pdf->lastPage();
if(!empty($pdfReportList)){
// 之后的每一页存放6个元素
$pageSize = 6;
$pages = [];
for ($i = 6; $i < count($pdfReportList); $i += $pageSize) {
$page = array_slice($pdfReportList, $i, $pageSize);
$pages[] = $page;
}
for ($i = 0; $i < count($pages); $i++) {
// file_put_contents('./pdf.json',$i . PHP_EOL, FILE_APPEND);
$pdf->AddPage();
$html = $this->twig->render('othersalary/pdf-sub.html.twig', ['list'=>$pages[$i]]);
$pdf->writeHTML($html, true, false, true);
$pdf->lastPage();
}
}
$autoPath = sprintf(CommonUtil::getEnvValue('BATCH_STAFF_SALARY_PDF_PATH'),date('Ymd'));
$dirPath = $this->kernel->getProjectDir() .$autoPath;
//创建磁盘目录
if(!$this->filesystem->exists($dirPath)){
$this->filesystem->mkdir(Path::normalize($dirPath));
}
$fileName = 'salaryrep'.$corpId.$monthly.'.pdf';
//数据库定义路径
$pdfPath = Path::normalize($autoPath.DIRECTORY_SEPARATOR.$fileName);
//写入本地文件
$localPath = $dirPath.DIRECTORY_SEPARATOR.$fileName;
$pdf->Output($localPath, 'F');
//更新salaryReport
if($salaryReport){
$salaryReport->setReportFile($pdfPath);
$salaryReport->setStatus(SalaryConst::SALARY_REPORT_STATUS_1);
$salaryReport->setUpdateUserId($this->getUserId());
$this->salaryReportRepository->add($salaryReport,true);
}
}
实现逻辑就是拿到数据按照PDF 每一个能存放多少个对象,需要两个静态html模板 第一个模板可能有头部一些信息,第二个模板纯列表展示