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

时间:2012-09-11 06:43:01

标签: php pdf-generation fpdf page-break

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

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');
    }

1 个答案:

答案 0 :(得分:3)

使用以下代码添加页面。

$pdf->AddPage();

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