编程语言
首页 > 编程语言> > 在pdf文档中插入手动分页符-PHP

在pdf文档中插入手动分页符-PHP

作者:互联网

我有3个数组($PSA,$NP和$OP),它们用于生成pdf.下面是我用来生成pdf的函数.问题是,数组中的所有值不能包含在一页中,因此需要添加分页符,但是当我尝试将SetAutoPageBreak设置为TRUE时,pdf无法加载.当它设置为False时(如方法中一样),我只会得到值的第一页(其余部分被截断).有没有办法手动设置分页符?我可以使用类似这样的条件:
if($pdf-> getPageHeight()-$GetY()< 5)添加分页符;
但不确定将代码放在哪里(我必须在输出每行之前检查条件?)
谢谢!

function generate_pvr($PSA, $NP, $OP, $FROM, $TO){
        $heading = 'Test [ From: '.$FROM.' To: '.$TO.' ]';
        $border = 0;

        $pdf = new TTPDF('P','mm','A4');
        $pdf->setMargins(0,0);
        $pdf->SetAutoPageBreak(FALSE);
        $pdf->AddPage();
        $adjust_x = 0;
        $adjust_y = 0;

        //Heading
        $pdf->SetFont('','B',12);
        $pdf->setXY( Misc::AdjustXY(30, $adjust_x), Misc::AdjustXY(20, $adjust_y) );
        $pdf->Cell(30,15,$heading, $border, 0);

        $block_adjust_y = 45;
        $block_adjust_x = 40;

        //PA entries
        foreach( $PSA as $key=>$value ) {             
            $block_adjust_y = $block_adjust_y + 5;
            $pdf->setXY( Misc::AdjustXY($block_adjust_x, $adjust_x), Misc::AdjustXY($block_adjust_y, $adjust_y) );
            $pdf->SetFont('','B',12);
            $pdf->Cell(45,5, $key, $border, 0, 'L');
            $pdf->setXY( Misc::AdjustXY($block_adjust_x + 20, $adjust_x), Misc::AdjustXY($block_adjust_y + 10, $adjust_y) );
            $pdf->SetFont('','I',10);
            $pdf->Cell(45,5, 'Emp#', $border, 0, 'L');
            $pdf->Cell(45,5, 'Employee Name', $border, 0, 'L');
            $pdf->Cell(45,5, 'Amount', $border, 0, 'R');
            $block_adjust_y = $block_adjust_y + 15;

            foreach ($value as $key2=>$value2){
                        $pdf->setXY( Misc::AdjustXY($block_adjust_x+20, $adjust_x), Misc::AdjustXY($block_adjust_y + 5, $adjust_y) );
                        $pdf->SetFont('','',10);
                        $pdf->Cell(45,5, $value2['emp_num'], $border, 0, 'L');
                        $pdf->Cell(45,5, $value2['user_name'], $border, 0, 'L');
                        $pdf->Cell(45,5, TTi18n::formatNumber( $value2['amount'] ), $border, 0, 'R');
                        $sumpsa = $sumpsa + $value2['amount'];
                        $block_adjust_y = $block_adjust_y + 5;



            }
            $pdf->setXY( Misc::AdjustXY($block_adjust_x+20, $adjust_x), Misc::AdjustXY($block_adjust_y + 5, $adjust_y) );
            $pdf->SetFont('','B',10);
            $pdf->Cell(90,5, 'Total', $border, 0, 'C');
            $pdf->Cell(45,5, TTi18n::formatNumber( $sumpsa ), $border, 0, 'R');
            $sumpsa = 0;
            $block_adjust_y = $block_adjust_y + 10;


    }


        //NP entries
        $pdf->setXY( Misc::AdjustXY($block_adjust_x, $adjust_x), Misc::AdjustXY($block_adjust_y + 5, $adjust_y) );
        $pdf->SetFont('','B',12);
        $pdf->Cell(45,5, 'NP', $border, 0, 'L');
        $pdf->setXY( Misc::AdjustXY($block_adjust_x + 20, $adjust_x), Misc::AdjustXY($block_adjust_y + 15, $adjust_y) );
        $pdf->SetFont('','I',10);
        $pdf->Cell(45,5, 'Emp#', $border, 0, 'L');
        $pdf->Cell(45,5, 'Employee Name', $border, 0, 'L');
        $pdf->Cell(45,5, 'Amount', $border, 0, 'R');
        $block_adjust_y = $block_adjust_y + 15;
        foreach ($NP as $keynp => $valuenp){
            $pdf->setXY( Misc::AdjustXY($block_adjust_x+20, $adjust_x), Misc::AdjustXY($block_adjust_y + 10, $adjust_y) );
            $pdf->SetFont('','',10);
            $pdf->Cell(45,5, $valuenp['employee_number'], $border, 0, 'L');
            $pdf->Cell(45,5, $valuenp['user_name'], $border, 0, 'L');
            $pdf->Cell(45,5, TTi18n::formatNumber( $valuenp['amount'] ), $border, 0, 'R');
            $sum_nopay = $sum_nopay + $valuenp['amount'];
            $block_adjust_y = $block_adjust_y + 5;
        }
        $pdf->setXY( Misc::AdjustXY($block_adjust_x+20, $adjust_x), Misc::AdjustXY($block_adjust_y + 10, $adjust_y) );
        $pdf->SetFont('','B',10);
        $pdf->Cell(90,5, 'Variation Total', $border, 0, 'C');
        $pdf->Cell(45,5, TTi18n::formatNumber( $sum_nopay ), $border, 0, 'R');
        $sum_nopay = 0;
        $block_adjust_y = $block_adjust_y + 10;

        //OP entries
        $pdf->setXY( Misc::AdjustXY($block_adjust_x, $adjust_x), Misc::AdjustXY($block_adjust_y + 5, $adjust_y) );
        $pdf->SetFont('','B',12);
        $pdf->Cell(45,5, 'Over Pay', $border, 0, 'L');
        $pdf->setXY( Misc::AdjustXY($block_adjust_x + 20, $adjust_x), Misc::AdjustXY($block_adjust_y + 15, $adjust_y) );
        $pdf->SetFont('','I',10);
        $pdf->Cell(45,5, 'Emp#', $border, 0, 'L');
        $pdf->Cell(45,5, 'Employee Name', $border, 0, 'L');
        $pdf->Cell(45,5, 'Amount', $border, 0, 'R');
        $block_adjust_y = $block_adjust_y + 15;
        foreach ($OP as $keyop => $valueop){
            $pdf->setXY( Misc::AdjustXY($block_adjust_x+20, $adjust_x), Misc::AdjustXY($block_adjust_y + 10, $adjust_y) );
            $pdf->SetFont('','',10);
            $pdf->Cell(45,5, $valueop['employee_number'], $border, 0, 'L');
            $pdf->Cell(45,5, $valueop['user_name'], $border, 0, 'L');
            $pdf->Cell(45,5, TTi18n::formatNumber( $valueop['amount'] ), $border, 0, 'R');
            $sum_overpay = $sum_overpay + $valueop['amount'];
            $block_adjust_y = $block_adjust_y + 5;
        }
        $pdf->setXY( Misc::AdjustXY($block_adjust_x+20, $adjust_x), Misc::AdjustXY($block_adjust_y + 10, $adjust_y) );
        $pdf->SetFont('','B',10);
        $pdf->Cell(90,5, 'Variation Total', $border, 0, 'C');
        $pdf->Cell(45,5, TTi18n::formatNumber( $sum_overpay ), $border, 0, 'R');
        $sum_overpay = 0;
        $block_adjust_y = $block_adjust_y + 10;            

        return $pdf->Output('','S');
    }

解决方法:

使用以下代码添加页面.

$pdf->AddPage();

使用上面的代码后,将在新页面中呈现对FPDF类的后续调用.

标签:fpdf,pdf-generation,page-break,php
来源: https://codeday.me/bug/20191127/2075918.html