0

我正在尝试使用 FPDF 类生成 pdf,但我遇到的问题是如何将从 mysql 数据库(查询的数据存储在数组中)获取的可变数据传递到生成的 pdf 中。

这是脚本中用于生成 pdf 的代码

        <?php
        @include_once("includes/db.php");
        require('fpdf/fpdf.php');
        @include_once("includes/course_info_query.php");

        $obj = new trainingCourses();
        $course_details = array();
        if(isset($_GET['c_id'])){
                $course_details = $obj->getPubCourseDetails($_GET['c_id']);
        }




        class PDF extends FPDF
        {   


    public $course_details;

    public function setData($input){
        $this->course_details = $input;
    }


        function Header()
        {
            // Logo
            $this->Image('s_pdf_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->course_details['comp_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->course_details['comp_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 BodyTop()
        {
        // 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,'Starting Date : ',0,0,'L');
        $this->Cell(30,8,$this->course_details['event_date'],0,0,'C');
        $this->SetX(150);
        $this->Cell(25,8,'Course Fee : ',0,0,'L');
        $this->Cell(20,8,$this->course_details['fee'],0,0,'C');
        $this->Ln();

        // Second Row
        $this->Cell(35,8,'Seating Capacity : ',0,0,'L');
        $this->Cell(30,8,$this->course_details['sit_capacity'],0,0,'L');
        $this->SetX(150);
        $this->Cell(25,8,'Duration : ',0,0,'L');
        $this->Cell(20,8,$this->course_details['duration'].' Day(s)',0,0,'L');
        $this->Ln();

        // Third Row
        $this->Cell(35,8,'Venue : ',0,0,'L');
        $this->Cell(30,8,$this->course_details['venue'],0,0,'L');
        $this->SetX(150);
        $this->Cell(25,8,'City : ',0,0,'L');
        $this->Cell(20,8,$this->course_details['city'],0,0,'L');
        $this->Ln();

        // Fourth Row
        $this->Cell(35,8,'Other Details : ',0,0,'L');
        $this->Cell(150,8,$this->course_details['other_det'],0,0,'L');
        $this->Ln();


        }


        function CourseBody()
        {

        $this->SetY(80);

        //$this->WriteHTML($html);
        $this->Write(10,$this->course_details['desc']);

        }

        function PrintChapter()
        {
            $this->AddPage();
            $this->BodyTop();
            $this->CourseBody();
        }
        }

        $pdf = new PDF();

$pdf->setData($course_details);
        //$pdf->Header();

        $pdf->SetAuthor($this->course_details['comp_name']);
        $pdf->PrintChapter();
        $pdf->Output();



        ?>

我希望得到一些帮助...谢谢

4

1 回答 1

4

它实际上只是基本的 OOP(面向对象编程)。

将变量添加到您的 PDF 类中,如下所示:

public $data;

在您的 PDF 类中添加一个函数,该函数接受一个参数并使用此函数设置上面的变量:

public function setData($input){
    $this->data = $input;
}

然后您就可以从您的 PDF 类中访问 $this->data。不要忘记实际调用您刚刚定义的函数(就在构造函数之后)。

编辑:要设置数据:

$pdf = new PDF();
$pdf->setData($course_details);

然后在您的 PDF 类中 $this->data 将是您的数组。您可能需要执行以下操作,以便了解数组的格式:

print_r($this->data);
于 2012-11-22T11:01:24.317 回答