2014-12-04 126 views
0

我剛剛開始使用FPDF,但我仍然不確定如何傳遞自己的變量。將PHP變量傳遞給FPDF的正確方法是什麼?

我想這樣做

的index.php:

... 
//set name variable 
$name = $_POST["name"] 

//embed the pdf 
<embed width="100%" height="100%" name="plugin" src="my-fpdf.php" type="application/pdf"> 
.... 

我-fpdf.php:

<?php 
    require('fpdf.php'); 

    $pdf = new FPDF(); 
    $pdf->AddPage(); 
    $pdf->SetFont('Arial','B',16); 
    $txt = "Hello ".$name."!" //access the variable 
    $pdf->Cell(40,10,$txt); 
    $pdf->Output(); 
?> 

是否有關於如何做到這一點的最佳做法?

回答

1

你需要將其添加爲查詢參數:

<embed ... src="my-fpdf.php?name="<?php echo $name ?>" ...> 

然後

$txt = "Hello, " . $_GET['name']; 
1

一個變量,您的PDF類添加爲這樣:

public $name; 

添加您的PDF類中的一個函數,它接受參數並使用此函數設置上面的變量:

public function setName($name){ 
    $this->name = $name; 
} 

然後,您將能夠從您的PDF類中訪問$ this-> name。不要忘記實際調用剛纔定義的函數(就在構造函數之後)。

$pdf = new PDF(); 
$pdf->setName('Some Name'); 
相關問題