2014-03-19 76 views
0

我正在使用FPDF生成報告到PDF文件,它的工作,但我想要的是打印名稱從變量報告的標題可以任何機構告訴我如何做到這一點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); 
+0

我使用FPDF類來生成PDF – user3355995

回答

0

我假設你的函數是一個類,那麼$ username屬性應與此$訪問的方法...

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

但在這種情況下,我認爲用戶名值應在受影響與直接在屬性初始化時不同的方式,因爲$ _POST值超出了Class。事情是這樣的:

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

UPDATE:

當你編輯你的代碼,我沒有看到你的頭()方法調用。 但是你可以通過用戶名值的方法PARAM這樣的:

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

你需要在PDF類中將global $username;添加到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;