php 使用tcpdf 实现pdf 分页导出

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模板 第一个模板可能有头部一些信息,第二个模板纯列表展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值