显示标题文本上生成的PDF与FPDF


Display Header Text on generated PDF with FPDF

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

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

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;

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

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

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

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

当您编辑代码时,我没有看到您对header()方法的调用。但是您可以像这样将username值作为方法参数传递:

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