简体   繁体   中英

FPDF - PHP inside WriteHTML function?

I have inserted some text in the WriteHTML function provided by http://www.fpdf.org/ under 'Tutorial 6: Links and flowing text'.

I want to add some php code inside of it but it does not quite function.

I tried the following but it just wrote the code too.

while ($row = mysql_fetch_array($result)) {

$html='Person:<b>".$row["firstname"] ." ". $row["lastname"]."</b>';

$pdf->AddPage();    
$pdf->SetXY(12, 127); 
$pdf->SetFontSize(11);
$pdf->WriteHTML(utf8_decode($html));
} $pdf->Output();

Any ideas? Is there any function in existance that can make this work?

Always be consistent with the use of quotes. If you open with a single-quote, close with a single-quote and if you open with a double-quote, close with a double-quote. Change the code to the following and it should work.

while ($row = mysql_fetch_array($result)) {

$html="Person:<b>".htmlspecialchars($row["firstname"])." ".htmlspecialchars($row["lastname"])."</b>";

$pdf->AddPage();    
$pdf->SetXY(12, 127); 
$pdf->SetFontSize(11);
$pdf->WriteHTML(utf8_decode($html));
} $pdf->Output();

Since everyone is ignoring my comments, I'll submit my own answer:

while ($row = mysql_fetch_array($result)) {
    $html="Person:<b>".htmlspecialchars($row["firstname"])." ".
         htmlspecialchars($row["lastname"])."</b>";

    $pdf->AddPage();    
    $pdf->SetXY(12, 127); 
    $pdf->SetFontSize(11);
    $pdf->WriteHTML(utf8_decode($html));
}
$pdf->Output();

Using htmlspecialchars ensures that names like O'Donnel or, worse, Mallory<evil code here> will not create invalid/dangerous HTML.

Note that what actually solves your problem is mismatched quotes:

$html='Person:<b>".$row["firstname"] ." ". $row["lastname"]."</b>';

verses:

$html="Person:<b>".$row["firstname"] ." ". $row["lastname"]."</b>";

If you change the setting of your $html variable to the following, your code should work correctly:

$html = 'Person:<b>' . $row['firstname'] . ' ' . $row['lastname'] . '</b>';

You were mixing single and double quotes incorrectly in your example.

The other option is to use double quotes, which allows variables to be parsed inside the quotes.

$html = "Person:<b> {$row['firstname']} {$row['lastname']} </b>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM