运行php脚本将html转换为pdf
我有一个表格,我想转换成pdf格式。我想用fpdf来做这件事 我的html代码如下: page.html运行php脚本将html转换为pdf,php,html,pdf,fpdf,Php,Html,Pdf,Fpdf,我有一个表格,我想转换成pdf格式。我想用fpdf来做这件事 我的html代码如下: page.html <!DOCTYPE html> <html> <head> </head> <body> <header> <h1 align='center'>Welcome </h1> </header> <section align = 'center'> <h2
<!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>
欢迎
登记
我在XAMPP上运行这个程序,并在htdocs中创建了一个名为demo的文件夹(其中有html文件、php文件和fpdf文件夹),但是当我在浏览器上运行html文件并单击register时,它只是在php文件中显示php代码,而不是生成pdf
这些文件位于文件夹demo中XAMPP文件的htdoc中,如下所示:
<?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();
}
?>
当我运行page.html时(单击register):
<?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();
}
?>
它向我展示了这一点:
<?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();
}
?>
为什么它不生成pdf 您需要输出到文件:
string Output([string dest [, string name [, boolean isUTF8]]])
Description
将文档发送到给定的目标:浏览器、文件或字符串。对于浏览器,可以使用PDF查看器,也可以强制下载。
如果需要终止文档,该方法首先调用Close()
看
查找手动菜单
描述
将文档发送到给定的目标:浏览器、文件或字符串。对于浏览器,可以使用PDF查看器,也可以强制下载。
如果需要终止文档,该方法首先调用Close()。
参数
目的地
发送文档的目的地。它可以是以下内容之一:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
name
The name of the file. It is ignored in case of destination S.
The default value is doc.pdf.
您需要输出到一个文件:
string Output([string dest [, string name [, boolean isUTF8]]])
Description
将文档发送到给定的目标:浏览器、文件或字符串。对于浏览器,可以使用PDF查看器,也可以强制下载。
如果需要终止文档,该方法首先调用Close()
看
查找手动菜单
描述
将文档发送到给定的目标:浏览器、文件或字符串。对于浏览器,可以使用PDF查看器,也可以强制下载。
如果需要终止文档,该方法首先调用Close()。
参数
目的地
发送文档的目的地。它可以是以下内容之一:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
name
The name of the file. It is ignored in case of destination S.
The default value is doc.pdf.
您的注册文件是
.php
扩展名还是.html
?什么是注册文件?下面的代码是我所有的代码都很好。我看到了编辑。我该如何创建它呢?到目前为止,我在这一行看到了一个问题:$pdf->Cell(10,10,“welcome{$f_name}”,1,0,C)
您有一个C
aka常量,但是您没有在php
文件中的任何地方定义它。所以定义或删除它:$pdf->Cell(10,10,“welcome{$f_name}”,1,0)代码>其次,您是否能够执行任何其他.PHP
文件?在我删除C之后,您的代码对我来说运行良好。
您的注册文件是.php
扩展名还是.html
?什么是注册文件?下面的代码是我所有的代码都很好。我看到了编辑。我该如何创建它呢?到目前为止,我在这一行看到了一个问题:$pdf->Cell(10,10,“welcome{$f_name}”,1,0,C)
您有一个C
aka常量,但是您没有在php
文件中的任何地方定义它。所以定义或删除它:$pdf->Cell(10,10,“welcome{$f_name}”,1,0)代码>其次,您是否能够执行任何其他.PHP
文件?在我删除了C.
我添加到我的答案后,你的代码对我来说运行良好。你应该查看我链接到的网站,了解这个库是如何工作的。完成文档创建后添加代码@joelgoldstick的意思是,更改$pdf->output()
到$pdf->Output('your file name.pdf','I')代码>CMIIWI添加到我的答案中。你应该查看我链接到的网站,了解这个库是如何工作的。完成文档创建后添加代码@joelgoldstick的意思是,更改$pdf->output()
到$pdf->Output('your file name.pdf','I')代码>CMIIW