Fpdf PHP page

0

I'm having trouble paging.

I get the data from the report in array format, so I have to do a foreach to scan the data, I happen to be doing it within 2 for, and the data keeps repeating, as I solve it, I tried to use some breaks, but not it works ?

namespace Helpers;

require_once 'Fpdf/fpdf.php';
require_once 'FormataData.php';
require_once 'C:xampp/htdocs/pim_dev/DAO/EstoqueDAO.php';

use \FPDF;
class RelatorioPorEntrada  {


    public function gerar($dataOne, $dataTwo, $dados)
    {


        $logo = "c:xampp/htdocs/pim_dev/content/img/relat4.png";
        $data = date('j/m/Y');
        $por_pagina = 3;
        $row = count($dados);
        $paginas = ceil($row/$por_pagina);
        $pdf = new FPDF("P", "cm", "A4");

        $linha_atual = 0;
        $inicio = 0;




        for($x=1; $x<=$paginas; $x++)
        {
            $inicio = $linha_atual;
            $fim = $linha_atual + $por_pagina;
            if($fim > $row) $fim = $row;
            $pdf->Open();
            $pdf->AddPage();
            $pdf->SetFont("arial");
            $pdf->Image($logo, 0,0);
            $pdf->SetTitle("Relatorio");

            $pdf->Ln(1);
            $pdf->SetFontSize(14);
            $pdf->Cell(0,3, utf8_decode("Relatório Mensal"),0,0,"C");
            $pdf->SetFontSize(10);
            $pdf->Cell(0, 5, utf8_decode("Página $x de $paginas"),0, 0, "R");
            $pdf->Ln(1);
            $pdf->Cell(0,3, "Data:".$data,0,0,"E");
            $pdf->Ln(1);
            $dateFormat = new FormataData();
            $novaDataone = $dateFormat->dateToBr($dataOne);
            $novaDataTwo = $dateFormat->dateToBr($dataTwo);
            $pdf->Cell(0,3,utf8_decode("Periodo:".$novaDataone." "."até ".$novaDataTwo),0,0,"E");
            $pdf->SetFontSize(8);
            $pdf->Ln(3);
            $pdf->SetDrawColor(57,181,74);
            $pdf->SetFillColor(57,181,74);
            $pdf->SetTextColor(255, 255, 255);
            $pdf->Cell(1,1, "ID", 1,0,"C",true);
            $pdf->Cell(3,1, "Produto", 1,0,"C",true);
            $pdf->Cell(3,1, "Date de Entrada", 1,0,"C",true);
            $pdf->Cell(2,1, utf8_decode("Valor Unitário"), 1,0,"C",true);
            $pdf->Cell(2,1, "Quantidade", 1,0,"C",true);
            $pdf->Cell(2,1, "Valor Total", 1,0,"C",true);
            $pdf->SetTextColor(0,0,0);




            for($i=$inicio; $i<$fim; $i++)
            {
                 foreach($dados as $values)
               {
                    $pdf->Ln(1);
                    $pdf->Cell(1,1, $dados[0], 1,0,"C");
                    $pdf->Cell(3,1, $dados[1], 1,0,"C");
                    $pdf->Cell(3,1, $dados[2], 1,0,"C");
                    $pdf->Cell(2,1, $dados[3], 1,0,"C");
                    $pdf->Cell(2,1, $dados[4], 1,0,"C");
                    $pdf->Cell(2,1, $dados[5], 1,0,"C");


                }


                $linha_atual++;
            }

        }
        return $pdf->Output("relatorio.pdf", "I");
    }


}

    
asked by anonymous 10.08.2017 / 14:36

1 answer

1

You could try using SetAutoPageBreak

$pdf->SetAutoPageBreak(true,10);

See if it helps.

Documentation link: link

Regarding Content Break:

The problem is that in the Cell () method called (MultiCell ()) the FPDF adds a new page if the current Y position + height of the new cell is greater than the height of the allowed page.

The default page height appears to be 297, with SetAutoPageBreak () you abstract 150 of it. So when Y + cell_height is greater than 147, you always get a new page when calling pages loop.

To avoid this, you need to call AddPage () by yourself. Add this check in your loop:

You can replace your $pdf->AddPage() with the information below.

$x = $this->width;
$y = $this->pdf->GetY();

if (($y + $this->line_height) >= 147) {
    $this->pdf->AddPage();
    $y = 0; // é sua margin top
}

Removing $pdf->SetAutoPageBreak(true,10);

    
10.08.2017 / 15:50