gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:47:21 25 4
gpt4 key购买 nike

我正在为 PDF 使用 FPDF,到目前为止,在我的搜索中,我看到了与我的问题类似的问题,并按照那里的代码运行它,当我运行它时,没有调用错误,可以下载 PDF。但问题是当我打开 PDF 文件时它是空的,我似乎无法理解这个问题。这是我的 PHP 代码:

<?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);
?>

最佳答案

这一行

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

将您的字体设置为当前字体

但是你之前没有设置任何字体,所以 fpdf 不知道要使用哪种字体。

因此,首先,您需要按照SetFont manual page 中的说明设置字体.

接下来是Cell功能。它的定义是

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

看到了吗?您要写入 pdf 的文本作为第三个参数。

在你的呼唤中

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

第三个参数是什么?我想是空字符串。

因此,其次,您需要修复您的 Cell 调用。

关于php - 使用php将mysql中的数据打印到pdf文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com