使用FPDF在生成的PDF上显示标题文本

时间:2014-03-19 13:15:36

标签: php

我正在使用FPDF生成报告到PDF文件并且它工作但我想要的是将名称从变量打印到报告的标题可以任何机构告诉我如何实现这个

我在标题上打印的变量是生成报告的人的用户名我尝试这样但是不起作用

<?php 
$username=$_POST['username'];

function Header()
{

    $name="Export PDF";
    $this->SetFont('Arial','B',10);
    $this->Image('images/pdflogo.png', 5,5,60);
    $this->Image('images/hr1.jpg', 10,25,190);
    $this->Text(100,25,'Weekly Report for user'.$username);

    $this->Cell(80);

    $this->SetFont('Arial','B',9);

    $this->Ln(20);
}

编辑:完整代码

class PDF extends FPDF {

    function Header() {

        $name = "Export PDF";
        $this->SetFont( 'Arial', 'B', 10 );
        $this->Image( 'images/pdflogo.png', 5, 5, 60 );
        $this->Image( 'images/hr1.jpg', 10, 25, 190 );
        $this->Text( 100, 25, 'Weekly Report'.$username );

        $this->Cell( 80 );

        $this->SetFont( 'Arial', 'B', 9 );

        $this->Ln( 20 );
    }


    function Footer() {
        $this->Image( 'images/hr1.jpg', 10, 280, 190 );
    }


    function LoadData( $file ) {

        $lines = file( $file );
        $data  = array();
        foreach ( $lines as $line ) {
            $data[ ] = explode( ';', chop( $line ) );
        }

        return $data;
    }


    function BasicTable( $header, $data ) {

        $this->SetFillColor( 255, 255, 255 );
        $this->SetDrawColor( 0, 0, 0 );
        $w = array( 20, 30, 25, 23, 30, 30, 30, 12, 30 );


        for ( $i = 0; $i < count( $header ); $i++ ) {
            $this->Cell( $w[ $i ], 7, $header[ $i ], 1, 0, 'C', true );
        }
        $this->Ln();


        foreach ( $data as $eachResult ) { //width

            $this->Cell( 20, 6, $eachResult[ "issue_title" ], 1 );
            $this->Cell( 30, 6, $eachResult[ "department" ], 1 );
            $this->Cell( 25, 6, $eachResult[ "submitted_by" ], 1 );
            $this->Cell( 23, 6, $eachResult[ "date" ], 1 );
            $this->Cell( 30, 6, $eachResult[ "solution_g" ], 1 );
            $this->Cell( 30, 6, $eachResult[ "t_status" ], 1 );
            $this->Cell( 30, 6, $eachResult[ "date_solved" ], 1 );

            $this->Ln();

        }
    }

}

$pdf = new PDF();
$header = array(
    'Issue Title',
    'Department',
    'Submited By',
    'Date',
    'Solution Given',
    'Ticket Status',
    'Date of Solved'
);
//Data loading
//*** Load MySQL Data ***//
$objConnect = mysql_connect( "localhost", "root", "" ) or die( "Error:Please check your database username & password" );
$objDB = mysql_select_db( "helpdesk_ticket" );
$strSQL = "SELECT issue_title, department,submitted_by,date,solution_g,t_status,date_solved FROM allocated_task where assigned_user ='$assigned_user' and date >= '$fyear-$fmonth-$fday' AND date <= '$tyear-$tmonth-$tday' ";
$objQuery = mysql_query( $strSQL );
$resultData = array();
for ( $i = 0; $i < mysql_num_rows( $objQuery ); $i++ ) {
    $result = mysql_fetch_array( $objQuery );
    array_push( $resultData, $result );
}
//************************//


function forme() {

}


$pdf->SetFont( 'Arial', '', 6 );

//*** Table 1 ***//

$pdf->AddPage();
//$pdf->Image('images/pdflogo.png',20,8,83);
//$pdf->Ln(15);

$pdf->BasicTable( $header, $resultData );
forme();
$pdf->Output( "$d.pdf", "F" );
$rep_count = mysql_query( $strSQL );
$con = mysql_num_rows( $rep_count );

2 个答案:

答案 0 :(得分:1)

您需要将global $username;添加到Header类中的PDF方法,以便它实际上可以在您的代码中使用。通常我会为班级中的用户名定义一个特殊字段,因为我认为这是一种更好的做法。像这样的东西

class PDF extends FPDF {

    private $username;

    public function getUsername(){
        return $this->username;
    }

    public function setUsername($username){
        $this->username;
    }
    // the rest of your code

}

然后只使用课程中的$this->username而不是定义global $username;

答案 1 :(得分:0)

我假设你的函数是一个Class的方法,那么$ username属性应该用$ this来访问...

$this->Text(100,25,'Weekly Report for user'.$this->username);

但是在这种情况下,我认为用户名值应该以不同于直接在属性初始化的方式受到影响,因为$ _POST值超出了Class。像这样:

$object = new YourClass();
$object->username = $_POST['username'];

更新:

在编辑代码时,我看不到你对header()方法的调用。 但您可以将用户名值作为方法参数传递:

    function Header($username){
        ...

        $this->Text(100,25,'Weekly Report for user'.$username);
    }

或在PDF类中添加一个属性,并使用this关键字访问它,如上所述:

class PDF extends FPDF
{
public $username ="";
...
...
$this->Text(100,25,'Weekly Report'.$this->username);