0

在一个项目上工作,需要使用 FPDF 生成产品详细信息的 pdf。产品详细信息被传递到一个数组中,我需要以下内容才能将数组“$prod_details”中的每个变量元素放入“PDF”类中的函数中,如下所示:

我如何尝试传递变量数组元素的示例:

    $this->Cell(30,8,$prod_data['prod_name'],0,0,'C');
    $this->Cell(30,10,$prod_data['company_name']);
    $this->Cell(20,8,$prod_data['prod_cost'],0,0,'C');

我尝试运行此脚本,但不断收到错误消息“无法访问空属性”...

找到下面的代码

<?php

@include_once("includes/db.php");
require('fpdf/fpdf.php');
@include_once("includes/class_product_info.php");

$obj = new allProducts();
$prod_data = array();
if(isset($_GET['c_id'])){
        $prod_data = $obj->getProdDetails($_GET['c_id']);

class PDF extends FPDF
{   


public $prod_data;

public function createData($input){
    $this->prod_data = $input;
}

function Header()
{
    // Logo
    $this->Image('big_logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',20);
    // Move to the right
    $this->Cell(40);    
    // Title
    $this->Cell(30,10,$this->prod_data['company_name']);
    // Draw an header line
    $this->Line(10,26,200,26);
    // Line break
    $this->Ln(20);
}

function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);

    // Begin with regular font
    $this->SetFont('Arial','',9);
    // Then put a blue underlined link
    //$this->SetTextColor(0,0,255);
    $this->SetFont('','U');
    $this->Write(10,$this->prod_data['company_name'],'http://www.skills.com');
    // Arial italic 8
    $this->SetFont('Arial','I',9);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}

function prodDetailTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();

/* Build cells for the first row */

$this->SetFont('Arial','',10);
$this->SetY(40);

// First Row
$this->Cell(35,8,'Product Name : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['prod_name'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Product Cost : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['prod_cost'],0,0,'C');
$this->Ln();

// Second Row
$this->Cell(35,8,'Discount : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['disc_amt'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'No Purchased : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['items_count'].' product(s)',0,0,'L');
$this->Ln();
}


function prodDetailBtm()
{

$this->SetY(80);

$this->Write(10,$this->prod_data['prod_desc']);

}

function generatePageData()
{
    $this->AddPage();
    $this->prodDetailTop();
    $this->prodDetailBtm();
}
}

$pdf = new PDF();
$pdf->createData($prod_data);
//$pdf->Header();

$pdf->generatePageData();
$pdf->Output();

}
else {

?>
    <script language="javascript">
    window.location = "prod_invoice_err.php";
    </script>
<?php
}
?>

希望能得到一些帮助。

4

1 回答 1

0

Your question is a little vague. It would be helpful it you asked specifically what you're trying to accomplish.

But the first thing I see is that your subclass of the fpdf class, you don't need to write functions to do each and everything you want to do with the pdf. You only need to extend the parts of the class you are overriding (or extending), like header and footer.

So extend it, manipulate header and footer, then close the class. Create your $pdf instance of your new fpdf class, then manipulate that object with your data. You don't need to 'pass in' that data at all.

for instance:

$pdf->Ln(10);
$pdf->Cell($w,9,$title,1,1,'C',true); //from fpdf tutorial

Or, if that doesn't accomplish what you want (although I can't see why it wouldn't, I've done this lots of times), you can always override the constructor. Pass in an array (or event a custom object that you create), and store that in a private variable.

于 2012-11-22T17:22:21.750 回答