0

我希望这个 php 变量显示在 pdf 文件中。但它给出了错误:- Pdf 文件不以 '%Pdf-' 开头

A.php 它包含我的表单。通过使用 post 我将值发送到其他 printpdf.php 其中 printpdf.php 生成具有特定格式的 pdf 文件。

一个.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>

打印pdf.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();
          ?>

错误:- 文件不以“%Pdf-”开头

4

4 回答 4

0

我建议避免使用hidden输入字段来解析变量。号召性用语时最好使用 url 本身:action="printpdf.php?cname=XXXX". 如果变量可能被用户破坏,请执行以下三件事之一: i) 将变量存储在服务器会话中:$_SESSION['cname']=XXXX;它几乎不能被操纵;ii) 对 url 字符串进行编码、散列或加密;iii) 在 printpdf.php 文件中恢复时清理变量。隐藏字段易于操作,是解析数据的原始方式。

于 2013-05-17T14:52:09.403 回答
0

试试这个方法:

<?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();
   }
      ?>
于 2013-05-17T14:52:35.267 回答
0

我也被困在我想生成带有一系列问题和选项的试卷的同一点。

无论你的 php 代码是什么,它都必须在require('fpdf.php');之前。

FPDF 不希望任何其他代码出现在他的代码之间。

我希望,这可以解决问题。

于 2016-08-09T08:29:43.027 回答
0

错误在这里

      $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();

将该行更改为$pdf->Cell(45,165,'', 1,0,'L');

注意你的参考,即我使用

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

需要($str);

因为我的代码在图像中,即 $this->Image($_SESSION['raiz'].'imagens/mmp.png',5,5,200); 如果您的 fpdf 库和图像在同一个文件夹中,那么一切都是正确的。顺便一提。我测试了代码正确的错误,它运行良好

于 2016-08-25T10:20:05.267 回答