Fpdf PHP page

Asked

Viewed 1,472 times

0

I’m having trouble making a pagination.

It receives the data of the report in array format, then I have to do a foreach to scan the data, it happens that I am doing it within 2 for, and the data keep repeating, as I solve already tried to use some break, but it does not work ?

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


}

distribuicao do texto

  • It would be interesting to add more content because this very vague your question

  • It is a pagination before generating the pdf.

  • My goal is to generate a pdf file without content breaks. When I start the text on a page I have to finish on this page.

  • Edit the question by adding information

  • I think I understand + or -. do you want it to be one table per page? If this is the case and in the Fpdf you can use CSS in the document configuration to generate the PDF, as in the DOMPDF example that "Transforms HTML and CSS into PDF", you could use the page-break-after and page-break-before property depending on your need. I hope that’s it.

1 answer

1


You could try using the Setautopagebreak

$pdf->SetAutoPageBreak(true,10);

See if it helps you.

Documentation link: http://www.fpdf.org/en/doc/setautopagebreak.htm

In relation to content break:

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

The height of the default page seems to be 297, with Setautopagebreak() you abstracted 150 from it. So when Y + cell_height is greater than 147, you always get a new page by calling loop of pages.

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

You can replace your $pdf->AddPage() by 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 the $pdf->SetAutoPageBreak(true,10);

  • It automatically breaks the page without Uebar the block content ??

  • @alexjosesilva Breaks automatically based on distance from the bottom set as example 10.

  • I place the call at the end of the loop...or before fpdf->output ?

  • @alexjosesilva puts after the $pdf->Open();

  • Can PHP inform the space occupied on the page or the space left on the page before arriving at the footer ??

  • @alexjosesilva If you take a look at the documentation you will see that the second parameter is the distance identifier with the bottom of the page. If it helped you the solution mark as solved.

  • the content is being broken at the end of the page. What I should do is make a pagination: know how many lines were generated and how many are missing. So I can avoid breaking the text block!

  • I’m editing the answer

  • @alexjosesilva see if it’s become clearer and more functional for you

  • great. just like that.. perfect: Congratulations!!!

  • I’m glad I helped @alexjosesilva

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.