【问题标题】:Download fpdf with a given name?下载具有给定名称的 fpdf?
【发布时间】:2020-03-10 12:07:24
【问题描述】:

我正在尝试下载 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()
{
    $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(); 

?>

【问题讨论】:

    标签: php mysql fpdf


    【解决方案1】:

    没关系,明白了。通过在名称中添加 $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”来解析它。
    • @thephper 嗨,谢谢。我很清楚这就是我创建登录页面的原因。我在顶部添加了一小段代码来检查会话是否开启。我没有把它包括在这里,因为我认为它不需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2013-04-19
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多