Php 下载具有给定名称的fpdf?
我正在尝试下载一个pdf文件。我可以下载它,但我希望文件名特定于MySQL数据。也就是说,在下面的代码中。我希望文件名是Php 下载具有给定名称的fpdf?,php,mysql,fpdf,Php,Mysql,Fpdf,我正在尝试下载一个pdf文件。我可以下载它,但我希望文件名特定于MySQL数据。也就是说,在下面的代码中。我希望文件名是$id中的数据。如何做到这一点 <?php //include connection file ob_start(); include_once('fpdf/fpdf.php'); include_once('connection.php'); class PDF extends FPDF { // Page header function Header() {
$id
中的数据。如何做到这一点
<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');
include_once('connection.php');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(276, 5, 'Details', 0, 0, 'C');
$this->Ln();
$this->SetFont('Times', '', 12);
$this->Cell(276, 10, 'Details of students', 0, 0, 'C');
$this->Ln(20);
}
// 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');
}
function headerTable()
{
$this->SetFont('Times', 'B', 12);
$this->Cell(40, 10, 'ID', 1, 0, 'C');
$this->Cell(20, 10, 'Name', 1, 0, 'C');
$this->Cell(90, 5, 'Score', 1, 0, 'C');
$this->Cell(30, 10, 'Percentage', 1, 0, 'C');
$this->Cell(20, 10, 'Age', 1, 0, 'C');
$this->Cell(0, 5, '', 0, 1, 'C');
//mini cell open
$this->Cell(60, 5, '', 0, 0, 'C');
$this->Cell(45, 5, 'Wrongs', 1, 0, 'C');
$this->Cell(45, 5, 'Rights', 1, 0, 'C');
//mini cell close
//$this->Cell(20, 10, 'Wrongs', 1, 0, 'C');
//$this->Cell(20, 10, 'Rights', 1, 0, 'C');
$this->Ln();
}
function viewTable($db)
{
$this->SetFont('Times', '', 12);
$id=$_GET['id'];
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
//$stmt->bindParam('s',$id);
//$stmt->query();
//$result=$stmt->get_result();
//$stmt->get_result();
while($data = $stmt->fetch(PDO::FETCH_OBJ)){
$this->Cell(40, 10, $data->ID, 1, 0, 'C');
$this->Cell(20, 10, $data->Name, 1, 0, 'L');
$this->Cell(45, 10, $data->Wrongs, 1, 0, 'L');
$this->Cell(45, 10, $data->Rights, 1, 0, 'L');
$this->Cell(30, 10, $data->Percentage, 1, 0, 'L');
$this->Cell(20, 10, $data->Age, 1, 0, 'L');
$this->Ln();
}
}
}
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->headerTable();
$pdf->viewTable($db);
$pdf->Output("D", "test.pdf"); //How to change test.pdf to $id.pdf? $id value obtained from viewTable()
ob_end_flush();
?>
别管了,明白了。在名称中添加$id很简单。完整的脚本:
<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');
include_once('connection.php');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(276, 5, 'Details', 0, 0, 'C');
$this->Ln();
$this->SetFont('Times', '', 12);
$this->Cell(276, 10, 'Details of students', 0, 0, 'C');
$this->Ln(20);
}
// 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');
}
function headerTable()
{
$this->SetFont('Times', 'B', 12);
$this->Cell(40, 10, 'ID', 1, 0, 'C');
$this->Cell(20, 10, 'Name', 1, 0, 'C');
$this->Cell(90, 5, 'Score', 1, 0, 'C');
$this->Cell(30, 10, 'Percentage', 1, 0, 'C');
$this->Cell(20, 10, 'Age', 1, 0, 'C');
$this->Cell(0, 5, '', 0, 1, 'C');
//mini cell open
$this->Cell(60, 5, '', 0, 0, 'C');
$this->Cell(45, 5, 'Wrongs', 1, 0, 'C');
$this->Cell(45, 5, 'Rights', 1, 0, 'C');
//mini cell close
//$this->Cell(20, 10, 'Wrongs', 1, 0, 'C');
//$this->Cell(20, 10, 'Rights', 1, 0, 'C');
$this->Ln();
}
function viewTable($db)
{
$this->SetFont('Times', '', 12);
$id=$_GET['id'];
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
//$stmt->bindParam('s',$id);
//$stmt->query();
//$result=$stmt->get_result();
//$stmt->get_result();
while($data = $stmt->fetch(PDO::FETCH_OBJ)){
$this->Cell(40, 10, $data->ID, 1, 0, 'C');
$this->Cell(20, 10, $data->Name, 1, 0, 'L');
$this->Cell(45, 10, $data->Wrongs, 1, 0, 'L');
$this->Cell(45, 10, $data->Rights, 1, 0, 'L');
$this->Cell(30, 10, $data->Percentage, 1, 0, 'L');
$this->Cell(20, 10, $data->Age, 1, 0, 'L');
$this->Ln();
}
}
}
$id=$_GET['id'];
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->headerTable();
$pdf->viewTable($db);
$pdf->Output("D", "$id.pdf");
ob_end_flush();
?>
很好,你知道了。您可能想了解sql注入,因为您直接在sql查询中解析$id变量-这可能是一个安全问题!如果你知道它只能是一个整数,那么在使用它时,通过键入“(int)$id”来解析它。我很清楚这就是为什么我创建了登录页面。我在上面添加了一小段代码来检查会话是否打开。我没有把它包括在这里,因为我认为不需要它。