<?php
require_once "FPDF/fpdf.php";
class PDF extends FPDF
{
    function Header()
    {
        global $titolo, $colonne, $intestaz, $misure, $allineam, $Ncolonne, $Tmisure, $HeaderCntrl;
        $Ncolonne = count($colonne);
        $Tmisure  = array_sum($misure);
//      TITOLO
        $this->SetTextColor(0);
        $this->SetFont('Helvetica','B',16);
        $this->Cell($Tmisure, 10, $titolo, 0, 1, 'L');
//      INTESTAZIONE COLONNE
        $this->SetFillColor(0,153,255);
        $this->SetTextColor(255);
        $this->SetFont('Helvetica','B',10);
        for( $i=0; $i<$Ncolonne; $i++ )
            $this->Cell($misure[$i], 6, $intestaz[$i], 1, 0, $allineam[$i], 1);
        $this->Ln();
//      SETTINGS FOR BODY and FOOTER PRINTING
        $HeaderCntrl = true;
    }
    function BodyTable()
    {
        global $colonne, $misure, $allineam, $Ncolonne, $Tmisure, $HeaderCntrl, $fill, $R;
//      CONTROLLA IL SALTO PAGINA
        $this->Cell(0, 6, ' ', 0, 0, 'L');
        $this->Ln(0);
//      INIZIALIZZA LA STAMPA SULLA NUOVA PAGINA
        if ($HeaderCntrl)
        {
            $fill = false;
            $this->SetFillColor(178,178,178);
            $this->SetTextColor(0);
            $this->SetFont('');
        }
//      STAMPA LA RIGA
        for($i=0; $i<$Ncolonne; $i++)
            $this->Cell($misure[$i], 6, $R[$colonne[$i]], 'LR', 0, $allineam[$i], $fill);
        $this->Ln();
//      flips from true to false and vise versa, line background color
        $fill =! $fill;
        $HeaderCntrl = false;
    }
    function Footer()
    {
        global $Tmisure;
//      CHIUDE LA STAMPA SULLA PAGINA
        $this->Cell($Tmisure, 0, '', 'T');
//      STAMPA IL PIEDE
        $this->SetY(-10);
        $this->SetFont('Helvetica','I',8);
        $this->Cell(0, 0, 'Nome della società', 0, 0, 'L');
        $this->Ln(0);
        $this->Cell(0, 0, 'Pagina '.$this->PageNo().'/{nb}', 0, 0, 'C');
        $this->Ln(0);
        $this->Cell(0, 0, date('e - l, F jS, Y - H:i:s'), 0, 0, 'R');
    }
}
$titolo="";
$colonne  = array('NumeroPratica' , 'DataSinistro' , 'OraSinistro' , 'LocalitaSinistro' , 'IndirizzoSinistro' );
$intestaz = array('Numero Pratica', 'Data Sinistro', 'Ora Sinistro', 'Localita Sinistro', 'Indirizzo Sinistro');
$misure   = array( 30             ,  30            ,  30           ,  50                ,  100                );
$allineam = array( 'L'            ,  'L'           ,  'L'          ,  'L'               ,  'L'                );
$sql = "Select * from stampaverbali order by Nominativo, NumPratica";
$sql = mysql_query($sql, $myconn) ;
$pdf = new PDF('L', 'mm', 'A4');
$pdf->SetFont('Times','',12);
$pdf->AliasNbPages();
while( $R = mysql_fetch_array($sql) )
{
    if( $titolo != $R['Nominativo'] )
    {
        $titolo = $R['Nominativo'];
        $pdf->AddPage();
    }
    $pdf->BodyTable();
}
$pdf->Output();
?>