douan4347 2018-02-17 18:02
浏览 110
已采纳

使用php将mysql中的数据打印成pdf文件

I am using FPDF for the PDF and so far in my search I saw this similar question to my question and followed the code there and when I ran it no Error was being called and the PDf can be downloaded. But the problem is when I open the PDF file it is empty and I can't seem to understand the problem. This is my PHP code:

<?php
include("connections.php");
require("fpdf181/fpdf.php");
$pdf = new FPDF(); 
$name = $address = $email = "";
$nameErr = $addressErr = $emailErr = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){ 
if(empty($_POST["name"])){
    $nameErr = "Name is required!";
}else{
    $name = $_POST["name"];
}
if(empty($_POST["address"])){
    $addressErr = "Address is required!";
}else{
    $address = $_POST["address"];
}
if(empty($_POST["email"])){
    $emailErr = "Email is required!";
}else{
    $email = $_POST["email"];
}   
}
//When button is clicked the pdf will be generated
if (isset($_POST["btn_1"])) {
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('','',12);

$view_query = mysqli_query($connections, "SELECT * FROM sample");
//where the database is accessed
while ($row = mysqli_fetch_assoc($view_query))
{
$db_name = $row["Name"];
$db_address = $row["Address"];
$db_email = $row["Email"];

$pdf->Cell(0,10,'', $db_name,1); 
$pdf->Ln();
$pdf->Cell(0,10,'', $db_address,1); 
$pdf->Ln();
$pdf->Cell(0,10,'', $db_email,1); 
$pdf->Ln();
}
$pdf->Output();

}
?>
<style>
.error{
color:red;  
}
</style>

<form method = "POST" action= "<?php htmlspecialchars("PHP_SELF");?>">

<input type = "text" name="name" value="<?php echo $name; ?>"><br>
<span class = "error"><?php echo $nameErr;?></span><br>

<input type = "text" name="address" value="<?php echo $address; ?>"><br>
<span class = "error"><?php echo $addressErr;?></span><br>

<input type = "text" name="email" value="<?php echo $email; ?>"><br>
<span class = "error"><?php echo $emailErr;?></span><br>

<input type = "submit" value="Submit">

<input type = "submit" name="btn_1" value="PDF">

</form>

<?php
// Insert to table
if($name && $address && $email){
$query = mysqli_query($connections, "INSERT INTO sample(Name,Address,Email) 
VALUES('$name','$address','$email')");

echo "<script language='javascript'>alert('New Record has been inserted!')
</script>";
echo"<script>window.location.href='index.php';</script>";
}
//view table
$view_query = mysqli_query($connections, "SELECT * FROM sample");
echo"<table border = '1' width ='50%'>";
echo "<tr>
<td>Name</td>
<td>Address</td>
<td>Email</td>
</tr>";
while ($row = mysqli_fetch_assoc($view_query)) {

$db_name = $row["Name"];
$db_address = $row["Address"];
$db_email = $row["Email"];
echo "<tr>
<td>$db_name</td>
<td>$db_address</td>
<td>$db_email</td>
</tr>";
    }
echo "</table>";
mysqli_close($connections);
?>
  • 写回答

1条回答 默认 最新

  • dongyou8701 2018-02-17 18:16
    关注

    This line

    $pdf->SetFont('','',12);
    

    sets your font to current font.

    But you didn't set any font earlier, so fpdf doesn't know, which font to use.

    So, first, you need to set font as described in SetFont manual page.

    Next, is Cell function. It's definition is

    Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
    

    See? Text which you want to write to pdf comes as third parameter.

    In you call

    $pdf->Cell(0,10,'', $db_name,1); 
    

    third parameter is what? Empty string, I suppose.

    So, second, you need to fix your Cell calls.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题