Generate dynamic table with TCPDF

Asked

Viewed 509 times

7

Good afternoon, I’m using the TCPDF to generate an evaluation report, and I need this report to be divided into weeks that the user has activity.

At the moment the report is thus Como o relatorio esta gerando

What I need is to go through the array, starting on Monday and ending on Sunday, when I arrive on Sunday I need to close the table and start a new one on Monday, generating again the header and the activities of the day, but I have no idea how to do this.


Function that generates the report

public function gerar_relatorio($__cod)
 {

    $sessao = $this->session->userdata('portal');

    $ativ_semanal = utf8_string_array_encode($this->atleta_model->get_atividade_semanal($__cod));

    $nome_atleta = $this->atleta_model->get_atleta($__cod);

    $borda = 0;

    $header_titulo = array('SEG','TER','QUA','QUI','SEX','SAB','DOM');

        for($j = 0; $j < count($header_titulo); ++$j) {
        $this->pdf->SetFillColor(0, 0, 255);
        $this->pdf->SetTextColor(255);
        $this->pdf->SetDrawColor(0, 0, 255);
        $this->pdf->SetLineWidth(0.3);
        $this->pdf->SetFont('', 'B');
        $this->pdf->Cell(25, 6, $header_titulo[$j], 1, 0, 'C', 1);
    }


        $this->pdf->Ln();

        $this->pdf->SetFillColor(224, 235, 255);
        $this->pdf->SetTextColor(0);
        $this->pdf->SetFont('');

           $ultimo_dow = 1;
    $x = 20;
    $y = 39;
    $contador = 0;
    $cont = 0;
    $ultimo_dia = 0;
    $ultimo_dia_semana = 0;
    $num_atividades_semana = 0;
    for ($i=0; $i < count($ativ_semanal); $i++) {
        $this->pdf->SetFillColor(255, 255, 255);
        $this->pdf->SetTextColor(0);
        $this->pdf->SetFont('');
        $num_atividades_semana = $ativ_max[$ativ_semanal[$i]->num_semana];
        if(($ativ_semanal[$i]->num_dia - $ultimo_dow) > 1){
            /* Esse bloco printa os intervalos maiores que 1 até que chegue em um dia preenchido */
            $cont = $ativ_max[$ativ_semanal[$i]->num_semana] - $contador;
            for ($j=0; $j < $cont; $j++) { 
                $this->pdf->SetFillColor(100, 100, 150); //Roxo
                $this->pdf->SetY($y);
                $this->pdf->SetX($x);
                $this->pdf->Multicell(25, 15, '', 1, 0, 'C', 1);
                $y += 15;
            }
            $ultimo_dow += 1;
            $ultimo_dia_semana = $ativ_semanal[$i]->num_dia;
            $x += 25;
            $y = 39;

            $contador = 0;
            if ($ultimo_dow != 7) {
                $i--;
            }
        }else{
            /* Printa a partir do ultimodow encontrado acima */
            if ($ativ_semanal[$i]->num_dia == $ultimo_dow) {
                $this->pdf->SetY($y);
                $this->pdf->SetX($x);
                $this->pdf->Multicell(25, 15, $ativ_semanal[$i]->descricao, 1, 0, 'C', 1);
                $y += 15;
                $contador+=1;

                /* Dia da semana que possui o maximo de atividades da semana */
                if (($ativ_max[$ativ_semanal[$i]->num_semana] - $contador) == 0) {
                    $contador = 0;
                    $y = 39;
                    $x += 25;
                    $ultimo_dow += 1;
                }
                $ultimo_dia_semana = $ativ_semanal[$i]->num_dia;
            }else {
                $cont = $ativ_max[$ativ_semanal[$i]->num_semana] - $contador;
                for ($j=0; $j < $cont; $j++) {
                    $this->pdf->SetFillColor(90, 255, 255); //Azul claro
                    $this->pdf->SetY($y);
                    $this->pdf->SetX($x);
                    $this->pdf->Multicell(25, 15, '', 1, 0, 'C', 1);
                    $y += 15;
                }
                $ultimo_dow = $ativ_semanal[$i]->num_dia;
                $x += 25;
                $y = 39;
                $contador = 0;
                $ultimo_dia_semana = $ativ_semanal[$i]->num_dia;
                if ($i != count($ativ_semanal)-1) {
                    $i--;
                }
            }
        }
    }

     if ($contador == 0) {
        $x += 25;
        $y = 39;
        $contador = $ultimo_dia_semana;
    }else{
        $x -= 25;
    }

    $x -= 25;
    $contador = $ultimo_dia_semana;
    for ($i=0; $i < (8-$contador); $i++) { 
        $cont = $contador - $num_atividades_semana;
        for ($j=0; $j < $cont; $j++) {
            $this->pdf->SetFillColor(0, 50, 255); //Azul Escuro
            $this->pdf->SetY($y);
            $this->pdf->SetX($x);
            $this->pdf->Multicell(25, 15, '', 1, 0, 'C', 1);
            $y += 15;
        }
        $x += 25;
        $y = 39;
        $contador = 0;
    }

    $this->pdf->Output($nome_atleta[0]->nome.'_'.date("d/m/Y").'.pdf', 'I');
    return;
 }

Return example with the data to mount the table

array(1) {
 [0]=>
  object(stdClass)#26 (7) {
   ["dia_semana"]=>
    string(7) "Segunda"
   ["num_dia"]=>
    string(1) "2"
   ["data_hora"]=>
    string(16) "26/11/2018 15:20"
   ["num_semana"]=>
    string(2) "48"
   ["descricao"]=>
    string(3) "Seg"
 }
}
  • 1

    Can you save the days of the week in numbers ? I think it gets easier, so you go through the array from 0 to 6 where 0 is Sunday, and then you break the report when you finish this one, generating a new line.

  • 1

    days of the week are being saved in numbers. But the week starts with 1 = Sunday

  • 2

    I’ll post it as a comment, because I’m aware that’s not what you asked. But after a long time, I decided to generate from html, and that made my life and development time much easier. Maintenance is much simpler too. https://tcpdf.org/examples/example_006/. Building dynamic HTML is easier than generating PDF directly

  • 1

    Without success even using HTML, the problem is in logic.

  • He’s a copy of fpdf: 8th! Only I think it’s more complete.

  • You can use mod: if ($i % 7 == 0) and do the continue; no for... hence you apply for each week, and start your array with $i = 1, do the for ($i=1; $i < count($days_month); $i++)

  • You can do something how that.

Show 2 more comments
No answers

Browser other questions tagged

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