FPDF Проблема загрузки фонового изображения со страницы, созданной - PullRequest
0 голосов
/ 25 сентября 2019

Я новичок в использовании FPDF и обнаружил некоторые трудности при попытке добавить фоновое изображение для каждой моей страницы.Количество страниц рассчитывается на основе второго столбца таблицы:

, если число строк culomn 2 превышает 23, тогда я создаю новую страницу

, затем яиспользуйте метод: SetAutoPageBreak (auto, 69)

Первая строка моей таблицы начинается с y = 112 и заканчивается y = 228

отсчет от нижней части страницы 297-228 =-69

Фоновое изображение, которое я использую. Похоже на это в размерах: enter image description here

Проблема в том, что я не могу понять, Каксделать на следующей странице мое фоновое изображение и написать на нем, как я делал на первой странице.

Вот код, который я до сих пор придумал:

Мои функции:

   <?php
require('fpdf/fpdf.php');

// Here connection To dataBase

$invoice_client = //fetch from database


$detail = $conn->query("select * from --"    );


$count_line=0;
class PDF extends FPDF
{
    var $ligne =0;
function info_Header()
{
    //Some Code Of InsertinG Header Code Here
    $this->Ln(19);
}

function info_Footer()
{

    global $invoice_client;

    $total = number_format($invoice_client->total, 2, ',', '');
    // total 
    $this->SetFont('Arial','B',11);
    $this->SetY(-55);
    $this->SetX(8);
    $this->Cell(40,5,iconv('UTF-8', 'windows-1252', $total),0,0,'C');
    //Rest Code Here    
}
function NbLines($w,$txt)
{
    //Calcule le nombre de lignes qu'occupe un MultiCell de largeur w
    $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;
}  


}

Рисование моего PDF:

$nb=0;
$pdf= new PDF();
$pdf->AliasNbPages();
$pdf->SetTopMargin(0);

$x0 = $pdf->GetX();
$y0 = $pdf->GetY();
$x = $pdf->GetX();
$y = $pdf->GetY();

$pdf->SetXY(0,$y);
$pdf->Image("invoice.jpg");
$pdf->info_Header();
$pdf->info_Footer();

//The First Line of my Table starts at y=112 and ends at y=228 (count from the bottom of the page 297-228 = -69)
$pdf->SetY(112);

while ($row = $detail->fetch_assoc())
{
    $query = $conn->query("select * from products where id=".$row['productID'] );
    if($query->num_rows >0)
    {
        $product = mysqli_fetch_object($query);
        $pdf->SetTextColor(65);

    $pdf->SetFont('Arial','',6.5);

         $pdf->SetX(8);
         $x=$pdf->GetX();
                $y=$pdf->GetY();
         $pdf->Cell(19,5,$product->num,0,0,'L');


         $pdf->MultiCell(77,5,strtoupper($product->name),0,'L');

        $val=strtoupper($product->name);


         $nb=$pdf->NbLines(77,$val);
         $h= ($nb)*5;
         if($row["detail"] != NULL && $row["detail"] != '')
         {
             $pdf->SetFont('Arial','',8);
             $pdf->SetXY($x+19,$y+$h); // x=8
             $pdf->MultiCell(77, 5, $row["detail"], 0, 'L');
             $nbb = $pdf->NbLines(77,$row["detail"]);


             }

         }else $nbb = 0;

         $h= $h+$nbb*5;
         $pdf->SetY($y+$h);

          $pdf->ligne = $pdf->ligne+$nb+$nbb;
          $pdf->count_line=$pdf->ligne;
          $_SESSION['count']=$pdf->count_line;
         $pdf->Ln(2.5);
         $pdf->SetAutoPageBreak(auto,69);    // When you need a page break   
    }  

$pdf->SetY(40);
$pdf->SetX(12);
$pdf->SetFont('Arial','B',11);

$pdf->count_line=$_SESSION['count'];

$pdf->MultiCell(85,4,'NOMBRE DE LIGNE: '.$pdf->count_line,0,'C');

$number = 23 ; //Number of Line That my table can Contain  
$a = $pdf->count_line/$number;   // Count All_Lignes/23 = number_of_Pages


if($a <= 1)
{
    $nbre_pages = 1;
}else
    $nbre_pages = ceil($a);

//See if the Number of Pages is Correct -> Delete Later
    $pdf->MultiCell(90,4,'NOMBRE DE Page: '.$a.' nb: '.$nbre_pages  ,0,'C');

//Trying to add the Image and header and footer to each page I have
    for ($i = 0; $i < $nbre_pages ; $i++)
    {  
            if ($i + 1 != $nbre_pages)
            {

                $x = $x0;
                $y = $y0;
                $pdf->SetXY(0,$y);
                $pdf->Image("invoice.jpg");
                $pdf->info_header();
                $pdf->info_footer();    
                $pdf->SetY(112);
            }}
$pdf->Output();
?>

Я не могу положить руку туда, где точно испортил,Я надеюсь, что кто-то может помочь мне, я буду очень признателен.

...