Скачать fpdf с указанным именем? - PullRequest
1 голос
/ 10 марта 2020

Я пытаюсь скачать PDF-файл. Я могу скачать его, но я хочу, чтобы имя файла указывало c на 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()
{
    $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(); 

?>

1 Ответ

2 голосов
/ 10 марта 2020

Неважно, понял. Это было просто, добавив $ 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(); 

?>
...