简体   繁体   English

运行PHP脚本将HTML转换为PDF

[英]running php script to convert html to pdf

I have a form which I want to convert to pdf. 我有一个要转换为pdf的表格。 I want to use fpdf to do that My html code is as follows: 我想使用fpdf做到这一点,我的html代码如下:

page.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>

I have created a php script to convert the form to pdf once the register button is clicked the php is : 一旦单击注册按钮,我创建了一个php脚本,将表格转换为pdf,php是:

form.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();

}
?>

I am running this on XAMPP and have created a folder called demo in htdocs(where the html file, php file and fpdf folder is there), however when I run html file on browser and click register, it simply displays the php code in the php file rather than generating the pdf 我在XAMPP上运行此文件,并在htdocs中创建了一个名为demo的文件夹(其中有html文件,php文件和fpdf文件夹),但是当我在浏览器中运行html文件并单击register时,它只是在php文件而不是生成pdf

The files are in htdoc of XAMPP files in folder demo like this: 文件位于文件夹演示中的XAMPP文件的htdoc中,如下所示:

在此输入图像描述

When I run page.html(click on register): 当我运行page.html(单击注册)时:

在此输入图像描述

It shows me this: 它向我展示了这一点:

在此输入图像描述

Why is it not generating a pdf? 为什么不生成pdf?

You need to output to a file: 您需要输出到文件:

string Output([string dest [, string name [, boolean isUTF8]]])
Description

Send the document to a given destination: browser, file or string. 将文档发送到给定的目的地:浏览器,文件或字符串。 In the case of a browser, the PDF viewer may be used or a download may be forced. 在浏览器的情况下,可以使用PDF查看器或强制进行下载。 The method first calls Close() if necessary to terminate the document. 必要时,该方法首先调用Close()以终止文档。

see http://www.fpdf.org/ 参见http://www.fpdf.org/

Look for the Manual menu 查找“手动”菜单

Description 描述

Send the document to a given destination: browser, file or string. 将文档发送到给定的目的地:浏览器,文件或字符串。 In the case of a browser, the PDF viewer may be used or a download may be forced. 在浏览器的情况下,可以使用PDF查看器或强制进行下载。 The method first calls Close() if necessary to terminate the document. 必要时,该方法首先调用Close()以终止文档。 Parameters 参数

dest Destination where to send the document. dest将文档发送到的目的地。 It can be one of the following: 可以是以下之一:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM