我用fpdf动态创建pdf。代码可能不是最漂亮的,但它或多或少都像我想要的那样。我遇到的问题是我想用<br />
替换从数据库中提取的所有内容中的"\n"
。每当我尝试自己添加和变量时,我都会收到
......中的意外变量
我的完整代码是:
<?php
//connection to database
require("db_link.inc.php");
//get correct Id
if(isset($_GET["Id"])){
$lpId = $_GET["Id"];
}
else {
header("Location: main.php");
}
//query
$sql_result = $link->query("SELECT * FROM LessonPlans WHERE Id=$lpId;") or die ("<div class='row'>
<div class='col s12 m6 offset-m3'>
<div class='card red'>
<div class='card-content white-text'>
<span class='card-title'><strong>Oh snap!</strong></span>
<p>We couldn't ask the query</p>
</div>
</div>
</div>");
require('../mlslp/assets/fpdf/fpdf.php');
class PDF extends FPDF
{
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
// Logo
$pdf->Image('../mlslp/assets/img/ML-logo.jpeg',7,6,30);
// Arial bold 18
$pdf->SetFont('Arial','B',18);
// Move to the right
$pdf->Cell(55);
// Title
while ($row = mysqli_fetch_array($sql_result)){
$pdf->Cell(80,20,$row['Subject'],0,0,'C');
// Move to the right
$pdf->Cell(25);
// Arial 12
$pdf->SetFont('Arial','',12);
// Level
$pdf->Cell(30,20,$row['Level'],0,0,'R');
// Line break
$pdf->Ln(25);
// Arial bold 12
$pdf->SetFont('Arial', 'B', 12);
// Background color
$pdf->SetFillColor(200,220,255);
// Aim heading
$pdf->Cell(0,10,'Aim',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Aim
$pdf->Cell(0,0,$row['Aim'],0,1,'C');
$pdf->Ln(10);
// Arial Bold 12
$pdf->SetFont('Arial', 'B', 12);
// Grammar heading
$pdf->Cell(95,10,'Grammar',0,0,'C',true);
// Vocabulary heading
$pdf->Cell(95,10,'Vocabulary',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Grammar
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->MultiCell(90,7,$row['Grammar'],0,'L');
// Move to the right
$pdf->SetXY($x + 100, $y);
// Vocabulary
$pdf->MultiCell(90,7,$row['Vocabulary'],0,'L');
$pdf->Ln(10);
// Arial Bold 12
$pdf->SetFont('Arial', 'B', 12);
// Procedure heading
$pdf->Cell(0,10,'Procedure',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Procedure
$pdf->MultiCell(190,7,$row['Proc'],0,'L');
$pdf->Ln(10);
// Arial Bold 12
$pdf->SetFont('Arial', 'B', 12);
// Exercise heading
$pdf->Cell(0,10,'Exercise',0,1,'C',true);
$pdf->Ln(6);
// Times 12
$pdf->SetFont('Times', '', 12);
// Exercise
$pdf->MultiCell(190,7,$row['Exercise'],0,'L');
}
$pdf->Ln(10);
$pdf->Output();
?>
同样,不是最漂亮的代码,但它有效。但是如果我想用换行符替换<br />
则不行。我该怎么做?
答案 0 :(得分:0)
这应该可行,将它放在循环之前:
<强> PHP 强>
$pdf->Cell(55);
$breaks = array("<br />","<br>","<br/>");
while ($row = mysqli_fetch_array($sql_result)){
foreach ($row as $key => $value) {
$row[$key] = str_ireplace($breaks, "\r\n", $value);
}
$pdf->Cell(80,20,$row['Subject'],0,0,'C');
//...