在使用fpdf生成的pdf文件中显示php变量

I want this php variable to be showed in pdf file . But it gives error :- Pdf file does not begin with '%Pdf-'

A.php it contains my form . By using post i am sending values to other printpdf.php Where printpdf.php generates a pdf file with a specific format .

A.php

 <form class="mid" action="printpdf.php" method="post"> 
 <input type="hidden" id="cname" name="cname" value="<?php echo $c_n[0]; ?>"/>
  <input type="submit" id="but" value="Print Challan"/>     
 </form>

printpdf.php

 <?php

  $c_n = $_POST['cname'];


  require('fpdf.php');
 class PDF extends FPDF
     {

     function Header()
    {
       $this->Image('image.jpg',5,5,200);
       $this->Ln(20);
       }

     //Page footer
     function Footer()
   {
       $this->SetFont('Arial','I',8);
       $this->Image('image1.jpeg',5,275,200);
       $this->SetXY(5, 284);
       $this->Cell(0,5,'This is a system generated ',0,2,'C');
       $this->Cell(0,5,'Page '.$this->PageNo().'/{nb}',0,0,'C');

       }
        }
         //Instanciation of inherited class

       $pdf=new PDF();
       $pdf->AliasNbPages();
       $pdf->AddPage();
       $pdf->SetFont('Times','B',15);
       $pdf->SetXY(5,42);
       $pdf->Cell(190,10,'Challan', 0,0,'C');
       $pdf->SetXY(5,52);
       $pdf->SetFont('Times','B',12);
       $pdf->Cell(150,18,'Name :-', 1,0,'L');


       $pdf->Cell(15,0,'$c_n', 0,0,'L');

       $pdf->SetXY(155,52);
       $pdf->Cell(50,9,'Challan No. :-', 1,2,'L');
       $pdf->Cell(50,9,'Date :-', 1,0,'L');
       $pdf->SetXY(5,70);
       $pdf->Cell(15,15,'Sno', 1,0,'C');
       $pdf->Cell(35,15,'Type', 1,0,'C');
       $pdf->Cell(45,15,'Make', 1,0,'C');
       $pdf->Cell(45,15,'Model', 1,0,'C');
       $pdf->Cell(45,15,'Serial No.', 1,0,'C');
       $pdf->Cell(15,15,'Qty', 1,0,'C');
       $pdf->SetXY(5,85);
       $pdf->SetFont('Times','',12);
       $pdf->Cell(15,165,'', 1,0,'L');
       $pdf->Cell(35,165,'', 1,0,'L');
       $pdf->Cell(45,165,'', 1,0,'L');
       $pdf->Cell(45,165,'', 1,0,'L');
       $pdf->Cell(45,165,, 1,0,'L');
       $pdf->Cell(170,165,'', 1,0,'L');
       $pdf->Cell(15,165,'', 1,0,'L');
        $pdf->SetXY(5,245);
       $pdf->SetFont('Times','B',12);
       $pdf->Cell(35,20,'Reciever Name :- ', 0,0,'L');
       $pdf->SetXY(125,245);
        $pdf->Cell(35,20,'Reciever Signature :- ', 0,0,'L');
       $pdf->Output();
          ?>

Error : - File doesnot begin with '%Pdf- '

I recommend avoid hidden input fields to parse variables. Better use the url itself when calling to action: action="printpdf.php?cname=XXXX". If the variable can be spoiled by the user, do one of three things: i)store the variable in server session: $_SESSION['cname']=XXXX; where it can't hardly be manipulated; ii)encode, hash or encrypt the url string; iii) sanitize the variable when recovering in the printpdf.php file. Hidden fields are easy to manipulate and are a primitive way of parsing data.

Try this way:

<?php
 require('fpdf.php');
 class PDF extends FPDF
 {

 function Header()
  {
   $this->Image('image.jpg',5,5,200);
   $this->Ln(20);
   }

 //Page footer
 function Footer()
 {
   $this->SetFont('Arial','I',8);
   $this->Image('image1.jpeg',5,275,200);
   $this->SetXY(5, 284);
   $this->Cell(0,5,'This is a system generated ',0,2,'C');
   $this->Cell(0,5,'Page '.$this->PageNo().'/{nb}',0,0,'C');

   }
    }
     //Instanciation of inherited class

   if(isset($_POST['cname']))
   {$c_n = $_POST['cname'];

   $pdf=new PDF();
   $pdf->AliasNbPages();
   $pdf->AddPage();
   $pdf->SetFont('Times','B',15);
   $pdf->SetXY(5,42);
   $pdf->Cell(190,10,'Challan', 0,0,'C');
   $pdf->SetXY(5,52);
   $pdf->SetFont('Times','B',12);
   $pdf->Cell(150,18,'Name :-'.$c_n, 1,0,'L');


   $pdf->Cell(15,0,$c_n, 0,0,'L');

   $pdf->SetXY(155,52);
   $pdf->Cell(50,9,'Challan No. :-', 1,2,'L');
   $pdf->Cell(50,9,'Date :-', 1,0,'L');
   $pdf->SetXY(5,70);
   $pdf->Cell(15,15,'Sno', 1,0,'C');
   $pdf->Cell(35,15,'Type', 1,0,'C');
   $pdf->Cell(45,15,'Make', 1,0,'C');
   $pdf->Cell(45,15,'Model', 1,0,'C');
   $pdf->Cell(45,15,'Serial No.', 1,0,'C');
   $pdf->Cell(15,15,'Qty', 1,0,'C');
   $pdf->SetXY(5,85);
   $pdf->SetFont('Times','',12);
   $pdf->Cell(15,165,'', 1,0,'L');
   $pdf->Cell(35,165,'', 1,0,'L');
   $pdf->Cell(45,165,'', 1,0,'L');
   $pdf->Cell(45,165,'', 1,0,'L');
   $pdf->Cell(45,165,, 1,0,'L');
   $pdf->Cell(170,165,'', 1,0,'L');
   $pdf->Cell(15,165,'', 1,0,'L');
    $pdf->SetXY(5,245);
   $pdf->SetFont('Times','B',12);
   $pdf->Cell(35,20,'Reciever Name :- ', 0,0,'L');
   $pdf->SetXY(125,245);
    $pdf->Cell(35,20,'Reciever Signature :- ', 0,0,'L');
   $pdf->Output();
   }
      ?>

I was also stuck at same point where i wanted to generate question paper with array of question and options.

Whatever you php code is, it must go before require('fpdf.php');

FPDF dont want any other code to come in between his code.

I hope, that solves the issue.

Error is here

      $pdf->Cell(35,165,'', 1,0,'L');
   $pdf->Cell(45,165,'', 1,0,'L');
   $pdf->Cell(45,165,'', 1,0,'L');//*
   $pdf->Cell(45,165,, 1,0,'L'); <<<<<<<<<<-----------------
   $pdf->Cell(170,165,'', 1,0,'L');
   $pdf->Cell(15,165,'', 1,0,'L');
    $pdf->SetXY(5,245);
   $pdf->SetFont('Times','B',12);
   $pdf->Cell(35,20,'Reciever Name :- ', 0,0,'L');
   $pdf->SetXY(125,245);
    $pdf->Cell(35,20,'Reciever Signature :- ', 0,0,'L');
    //*/
   $pdf->Output();

change that line to $pdf->Cell(45,165,'', 1,0,'L');

put attention to your reference i.e. i use

 $str = '../fpdf/fpdf.php';

require($str);

because there is where my code is an in images i.e. $this->Image($_SESSION['raiz'].'imagens/mmp.png',5,5,200); if you have your fpdf library and images in the same folder then all is right. by the way. i test the code correct error and it runs well