我正在使用来自MySQL的数据使用fpdf和php动态创建pdf。我的问题是数据库有ascii字符(如#039;
等),我想将其更改为可读的UFT-8。 html_entity_decode()
应该是这个工具,但我不知道我的代码应该放在哪里使它适用于整个文档。我的代码如下:
<?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);
// remove <br>-code from pdf output
$breaks = array("<br />","<br>","<br/>");
while ($row = mysqli_fetch_array($sql_result)){
foreach ($row as $key => $value) {
$row[$key] = str_ireplace($breaks, "", $value);
}
// Title
$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();
?>
我知道代码不是最好的代码,但除html_entity_decode()
问题外,其他一切都应该如此。那么我该如何编写这个来处理pdf中的所有字段?