0

I'm working on TCPDF library and I get the error message ( Undefined variable: rand in D:\xampp\htdocs\test\lib\examples\test.php on line 43 ) although it is defined in footer section line 10.

<?php
 require_once('tcpdf_include.php');
 class MYPDF extends TCPDF {
  public function Header() {
    $image_file = K_PATH_IMAGES.'02.png';       $this->Image($image_file, 134.5, 16, 74, 28.5, 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
  }
  public function Footer() {
    $rand = rand(0,9999);
    $this->SetFont('arial', '', 10);
    $this->SetXY(25, 25);       
    $this->Cell(20, 0, $rand, 0, 0, 'R', 0, '', 0, false, 'M', 'M');
  }
 }

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);  $pdf->SetAuthor('ِِAbdulkarim Salem Alharbiِ'); $pdf->SetTitle('Taareef');  
$pdf->SetSubject('Taareef');    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, 68, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);     $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$lg = Array();      
$lg['a_meta_charset'] = 'UTF-8';        
$lg['a_meta_dir'] = 'rtl';      
$lg['a_meta_language'] = 'fa';      
$lg['w_page'] = 'page'; 
$pdf->setLanguageArray($lg);

if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// Header and Footer Section Ends Here--------------------------------------------------------- Main Body Starts
    $pdf->SetFont('arial', '', 10);
    $pdf->AddPage();
    $pdf->SetXY(50, 50);        
    $pdf->Cell(20, 0, $rand, 0, 0, 'R', 0, '', 0, false, 'M', 'M');
    $pdf->Output('hello', 'I');
?>

I need the variable to be passed to the following file parts

A.K.
  • 141
  • 1
  • 2
  • 14
  • `$rand` is a local variable in the `Footer()` method. – Barmar Nov 07 '19 at 23:09
  • You also never call `$pdf->Footer()`. – Barmar Nov 07 '19 at 23:09
  • @Barmar I moved out of the function and still the same issue – A.K. Nov 07 '19 at 23:16
  • Variables don't go away by themselves. If you assigned the variable in the top-level scope before you use it, it should be defined. – Barmar Nov 08 '19 at 00:38
  • @Barmar you are right in a common php file but with TCPDF, it doesn't work. the way I solved it is by adding global before when defining and referring to the variable global $rand ; – A.K. Nov 08 '19 at 19:45

0 Answers0