【发布时间】:2016-06-09 18:43:29
【问题描述】:
我有一个要转换为 pdf 的表格。我想使用 fpdf 来做到这一点 我的html代码如下:
page.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<h1 align='center'>Welcome </h1>
</header>
<section align = 'center'>
<h2> Register </h2>
<form action = "form.php" method = "post"
<p>
<label>First Name :</label>
<input type="text" name = "first_name" />
</p>
<p>
<label>Last Name :</label>
<input type="text" name="last_name" />
</p>
<p>
<label>Gender :</label>
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</p>
<p>
<label>Date of Birth :</label>
<input type="date" name="dob" />
</p>
<label>Mobile :</label>
<input type="text" name="mobile" />
</p>
<p>
<label>Email :</label>
<input type="text" name="email" />
</p>
<input type="submit" value="Register" name="submit" />
</form>
</section>
</body>
</html>
我已经创建了一个 php 脚本,一旦点击注册按钮,就可以将表单转换为 pdf,php 是:
form.php:
<?php
if(!empty($_POST['submit']))
{
$f_name=$_POST['first_name'];
$l_name=$_POST['last_name'];
$gender=$_POST['gender'];
$dob=$_POST['dob'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","B",16);
$pdf->Cell(10,10,"welcome {$f_name}",1,0,C);
$pdf->Cell(50,10,"Name :",1,0);
$pdf->Cell(50,10,"$l_name",1,0);
$pdf->Cell(50,10,"gender :",1,0);
$pdf->Cell(50,10,"$gender",1,1);
$pdf->Cell(50,10,"Mobile :",1,0);
$pdf->Cell(50,10,"$mobile",1,1);
$pdf->Cell(50,10,"Email :",1,0);
$pdf->Cell(50,10,"$email",1,1);
$pdf->output();
}
?>
我在 XAMPP 上运行它并在 htdocs 中创建了一个名为 demo 的文件夹(其中 html 文件、php 文件和 fpdf 文件夹在那里),但是当我在浏览器上运行 html 文件并单击注册时,它只是显示 php php文件中的代码而不是生成pdf
文件位于 demo 文件夹中 XAMPP 文件的 htdoc 中,如下所示:
当我运行 page.html(点击注册)时:
它向我展示了这个:
为什么不生成pdf?
【问题讨论】:
-
您的注册文件是
.php扩展名还是.html? -
什么是注册文件?以下代码是我编写的所有代码
-
好的。我看到了修改。
-
如何创建?
-
到目前为止我看到一个问题,在这一行:
$pdf->Cell(10,10,"welcome {$f_name}",1,0,C);你有一个C又名常量,但你没有在php文件的任何地方定义它。所以定义它或删除它:$pdf->Cell(10,10,"welcome {$f_name}",1,0);.其次,你能执行任何其他.PHP文件吗?在我删除C.之后,您的代码对我来说很好用