php - 如何将可变数据从数组传递到php中的fpdf

标签 php mysql pdf

我正在尝试使用 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();



        ?>

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

最佳答案

这实际上只是基本的 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);

关于php - 如何将可变数据从数组传递到php中的fpdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13510929/

相关文章:

php - PHP 是否有 date() 函数的反函数(strtotime() 除外)?

java - Apache FOP - PDF 创建俄语文本

php - mysql返回空集

mysql - Wordpress 查询不工作 - 没有错误 - 否则查询正常

php - 如果日期和时间不是TIMESTAMPDIFF格式那么如何用mysql找到间隔?

ruby - 解析PDF去除月份

java - batik :Linux 上的 svg 到 pdf(无 X 服务器)

php - 访问 Web 服务器文档根文件夹上方的文件

php - 设置不安全属性失败

php - 使用 javascript 和 php 增加一个值并将其保存在 POST 数组中