FPDF Generate a page for each row of the BD [closed]

0

I already have my code with FPDF to generate the sheet in the PDF document. Now, I would like to implement a loop that in the same PDF file for each row of the BD will generate a page in the document.

I think we should generate a function that calls the code that has the FPDF from the AddPage ().

Have you ever done something similar? Can you guide me?

Imagine that the data DB contains the following data:

  

Food | Expiration
  Cheese | 1 month
  Yogurt | 15 days
  Chicken | 3 days

What I want is for the PDF that generates FPD to have 3 pages, one for each row of the DB.

Thank you very much.

    
asked by wiki 07.03.2018 в 20:25
source

1 answer

0

You have to generate a function as I said, I leave the code, I hope it serves you:

<?php

header("Content-Type: text/html;charset=utf-8");

require('./fpdf181/fpdf.php');

class PDF extends FPDF
{
var $widths;
var $aligns;

function SetWidths($w)
{
    $this->widths=$w;
}

function SetAligns($a)
{
    $this->aligns=$a;
}

function Row($data)
{
    $nb=0;
    for($i=0;$i<count($data);$i++)
        $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
    $h=8*$nb;
    $this->CheckPageBreak($h);
    for($i=0;$i<count($data);$i++)
    {
        $w=$this->widths[$i];
        $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
        $x=$this->GetX();
        $y=$this->GetY();
        $this->Rect($x,$y,$w,$h);
        $this->MultiCell($w,8,$data[$i],0,$a);
        $this->SetXY($x+$w,$y);
    }
    $this->Ln($h);
}

function CheckPageBreak($h)
{
    if($this->GetY()+$h>$this->PageBreakTrigger)
        $this->AddPage($this->CurOrientation);
}

function NbLines($w,$txt)
{
    $cw=&$this->CurrentFont['cw'];
    if($w==0)
        $w=$this->w-$this->rMargin-$this->x;
    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
    $s=str_replace("\r",'',$txt);
    $nb=strlen($s);
    if($nb>0 and $s[$nb-1]=="\n")
        $nb--;
    $sep=-1;
    $i=0;
    $j=0;
    $l=0;
    $nl=1;
    while($i<$nb)
    {
        $c=$s[$i];
        if($c=="\n")
        {
            $i++;
            $sep=-1;
            $j=$i;
            $l=0;
            $nl++;
            continue;
        }
        if($c==' ')
            $sep=$i;
        $l+=$cw[$c];
        if($l>$wmax)
        {
            if($sep==-1)
            {
                if($i==$j)
                    $i++;
            }
            else
                $i=$sep+1;
            $sep=-1;
            $j=$i;
            $l=0;
            $nl++;
        }
        else
            $i++;
    }
    return $nl;
}

    }


//**RECOGIDA DE DATOS
//-------------------

include"./conectar.php";
$enlace= conectarse();

mysql_query("SET NAMES utf8");


$consulta = "SELECT * FROM producto";

$result=mysql_query($consulta,$enlace);
$number = mysql_numrows($result);

$alimento = "";
$caducidad= "";

$i=0;

while($row = mysql_fetch_array($result))
{
    $alimento[$i] = $row["alimento"];
    $caducidad[$i] = $row["caducidad"];
    $i++;

}
mysql_close();


function nuevaPagina($pdf,$alimento,$caducidad)
{
    $pdf->AddPage(); // agregamos la pagina
    $pdf->SetMargins(12,3,0); // definimos los margenes en este caso estan en milimetros
    $pdf->SetFont('Arial','B',10);
    $pdf->SetFillColor(223,245,222); 
    $pdf->Cell(23,8,utf8_decode("Alimento"), 1, 0, 'L','true'); 
    $pdf->SetXY($pdf->GetX(),$pdf->GetY()); 
    $pdf->SetFont('Arial','B',9); 
    $pdf->Cell(27,8,utf8_decode($alimento),1,0,'L','true');  
    $pdf->SetXY($pdf->GetX(),$pdf->GetY()); 
    $pdf->SetFont('Arial','',8); 
    $pdf->Cell(13,8,utf8_decode("Caducidad"),1,0,'L','true');  
    $pdf->SetXY($pdf->GetX(),$pdf->GetY()); 
    $pdf->SetFont('Arial','B',9); 
    $pdf->Cell(21,8,utf8_decode($caducidad),1,1,'L',true);  
}


$pdf=new PDF('P','mm','A5');


for($i=0;$i<$number-1;$i++)
{
    nuevaPagina($pdf,$alimento[$i],$caducidad[$i]);
}

$pdf->Output();
    
answered by 07.03.2018 / 20:57
source