我有一个PHP代码,可以获取数据库中的数据并显示。现在,我希望它以pdf格式显示

时间:2016-01-06 06:09:08

标签: php pdf

如何以php格式显示数据为pdf文件格式...我使用的是fpdf,但我不知道我的数据如何以pdf格式显示......

只是创建pdf的一个例子但是,我不知道如何使用php while循环,我得到错误......

$pdf = new FPDF();
    $pdf->AddPage();

    $pdf->SetFont("Arial","B","20");
    $pdf->Cell(150,10,"My DATAS",1,1,"C");
    $pdf->Cell(150,10,"My data Loop",1,1,"C");

    $pdf->Output();

这是我的PHP代码

<?php

require('fpdf.php');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rmsdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$from = '16/12/2015';
$to = '17/12/2015';
$sql = "select id, firstname, lastname, gender from walkin_member where datestamp between '$from' and 
'$to' order by datestamp asc";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    $row_cnt = $result->num_rows;
        echo $row_cnt;
    while($row = $result->fetch_assoc()) {


        $show = "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
        echo $show;





    }
} else {
    echo "0 results";
}

$conn->close();

2 个答案:

答案 0 :(得分:1)

你好我给你一个使用php生成pdf格式的演示,并在分析了你可以根据你想要生成的数据自己生成pdf。

假设我有一个HTML文件 pdf.html

<!doctype html>
<html>
<body>
<form action="pdf.php" method="post">
name:<input type=text name=t1 id=i1>
<br/>
password:<input type=text name=t2 id=i2>
<br/>
<input type=submit name=go value=pdf_generation>
</form>
</body>
</html>

现在,当我们点击提交按钮时,它将重定向到 pdf.php ,其中包含以下代码 -

<?php
extract($_REQUEST);
require('fpdf17/fpdf.php');
if(isset($go))
{
$pod=new FPDF();
$pod->AddPage();
$pod->setFont("Arial","B",16);
$pod->cell(100,10,"welcome",1,1);
$pod->cell(50,10,"Name:",1,0);
$pod->cell(50,10,"$t1",1,1);
$pod->cell(50,10,"Password:",1,0);
$pod->cell(50,10,"$t2",1,1);
$pod->output();
}
?>

如果你发现它很有用,请随意接受我的回答来解释我的答案。

答案 1 :(得分:1)

你可以试试这个

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
if ($result->num_rows > 0) {
    // output data of each row
    $row_cnt = $result->num_rows;

    while($row = $result->fetch_assoc()) {

        $show= "officeCode: " . $row["officeCode"]. " - city: " . $row["city"]. " " . $row["phone"]. "\n";
        $pdf->Cell(150,10,"$show",1,1,"C");

    } 
}
ob_end_clean(); //if Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file'
$pdf->Output();