Robson
Robson

Reputation: 1

PHP FPDF page break

I am using PHP and FPDF to generate a PDF with a list of items. I have a loop and in the loop I have a verification with GetY() to check when will be the break. This work fine but on the first page, no. It's creating pages, but in the first page no break the texto to second page like others. What should I do?

[...]
if ($pdf->GetY() > 230) {
     $pdf->AddPage();
} 
endwhile;

Full Code:

if($_GET['print'] == 'ok'){
$idO = $_GET['id'];
require_once "assets/fpdf/fpdf.php";

$pdf = new FPDF( 'P', 'mm', 'A4' );

$pdf->AddPage();
$pdf->SetFillColor( 255, 255, 255 );
$pdf->SetDrawColor( 255, 255, 255 );
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true, 30);

$itens = get_field('itens', $idO);
$args = array(
    'post_type' => 'categorias',
    'posts_per_page' => -1,
    'meta_key' => 'ordem',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        'key' => 'status',
        'value' => 'ativo',
        'compare' => '='
    )
);

$query = new WP_Query($args);
$i=0;
while ($query->have_posts()):$query->the_post();
    foreach ($itens as $key => $value){
        if(get_field('categoria', $value) == get_the_ID()){
            $i++;

            $pdf->SetFont( 'Arial', 'B', 10 );

            if($i == 1){
                $pdf->MultiCell( 0, 10, utf8_decode(html_entity_decode(get_the_title($value))).':', 0, 'C', false );
                $imgWidth = 65;
                $f = get_field('principal', $value);
                $wI = get_field('_wp_attachment_metadata', get_field('foto'.$f, $value))['width'];
                $hI = get_field('_wp_attachment_metadata', get_field('foto'.$f, $value))['height'];

                $pY = ($imgWidth * $hI) / $wI;

                $pX = ($pdf->GetPageWidth() - $imgWidth) / 2;

                $img = wp_get_attachment_image_src( get_field('foto'.$f, $value), 'full' )[0];
                $pdf->Image($img,$pX,$pdf->GetY(),$imgWidth);
                $pdf->Ln( $pY );

                $pdf->MultiCell( 0, 5, "Gabinete:", 0, 'L', false );
            } else{
                $pdf->MultiCell( 0, 5, utf8_decode(html_entity_decode(get_the_title($value))).':', 0, 'L', false );
            }

            if(get_field('modelo', $value) != ''){
                $pdf->Ln(2);
                $pdf->SetX(20);
                $pdf->SetFont( 'Arial', 'B', 10 );
                $pdf->MultiCell( 0, 5, "Modelo: ", 0, 'L', false );
                $pdf->SetX(25);
                $pdf->SetFont( 'Arial', '', 10 );
                $pdf->MultiCell( 0, 5, utf8_decode(get_field('modelo', $value)), 0, 'L', false );
            }
            if(get_field('fabricante', $value) != ''){
                $pdf->Ln(2);
                $pdf->SetFont( 'Arial', 'B', 10 );
                $pdf->SetX(20);
                $pdf->MultiCell( 0, 5, "Fabricante: ", 0, 'L', false );
                $pdf->SetX(25);
                $pdf->SetFont( 'Arial', '', 10 );
                $pdf->MultiCell( 0, 5, utf8_decode(get_field('fabricante', $value)), 0, 'L', false );
            }

            $pdf->SetX(20);
            $pdf->SetFont( 'Arial', 'B', 10 );
            $pdf->MultiCell( 0, 10, utf8_decode("Especificações Técnicas:"), 0, 'L', false );
            $pdf->SetX(25);
            $pdf->SetFont( 'Arial', '', 10 );
            $pdf->MultiCell( 0, 5, utf8_decode(html_entity_decode(get_field('descricao', $value))), 0, 'L', false );

            if($i > 1){
                if(get_field('exibir', $value) != ''){
                    $pdf->Ln( 2 );
                    $x=0;
                    foreach (get_field('exibir', $value) as $key2 => $value2){
                        $x++;
                        $imgWidth2 = 20;
                        $wtP = ($imgWidth2 * $x);
                        $pX2 = ($x == 1)?($pdf->GetX() + $wtP):($pdf->GetX() + $wtP + 5);
                        $img2 = wp_get_attachment_image_src( get_field('foto'.$value2, $value), 'full' )[0];
                        $pdf->Image($img2,$pX2,$pdf->GetY(),$imgWidth2);
                    }
                    $pdf->Ln( 25 );
                }
            }

            if ($pdf->GetY() > 265) {
                $pdf->AddPage();
            }
        }
        $pdf->Ln( 2 );
    }
endwhile;
wp_reset_query();

$pdf->Output('', 'relatorio.pdf');
die();

}

Upvotes: 0

Views: 2160

Answers (2)

Edi
Edi

Reputation: 645

Instead make an IF() and use GETY () because you do not use SETY ()? Creating a footer.

instead of:

if ($pdf->GetY() > 265) {
     $pdf->AddPage();
}

do that:

// set your value
$this->SetY(-15);
$pdf->AddPage();

Look at this official example

Upvotes: 2

Robson
Robson

Reputation: 1

seems that "SetAutoPageBreak" isn't work on the first page, only on the others. The code is creating pages, but don't breaking on the first

Upvotes: 0

Related Questions