doudeng9425 2016-05-22 13:20
浏览 33

如何在fpdf中使用html_entity_decode()

I'm creating pdf's dynamically using fpdf and php with data from MySQL. My problem is that the database had ascii characters (like #039; etc) and I want to change that to readable UFT-8. html_entity_decode() should be the tool for this but I don't know where in my code I should put it to make it work for the whole document. My code is following:

<?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&apos;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();  
?>

I know the code is not the best written, but except for the html_entity_decode() problem everything else comes out like it should. So how do I code this to work on all fields in the pdf?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看