【问题标题】:running php script to convert html to pdf运行 php 脚本将 html 转换为 pdf
【发布时间】: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-&gt;Cell(10,10,"welcome {$f_name}",1,0,C); 你有一个C 又名常量,但你没有在php 文件的任何地方定义它。所以定义它或删除它:$pdf-&gt;Cell(10,10,"welcome {$f_name}",1,0);. 其次,你能执行任何其他.PHP 文件吗?在我删除 C. 之后,您的代码对我来说很好用

标签: php html pdf fpdf


【解决方案1】:

你需要输出到一个文件:

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

将文档发送到给定的目的地:浏览器、文件或字符串。在浏览器的情况下,可以使用 PDF 查看器或强制下载。 如果需要终止文档,该方法首先调用 Close()。

http://www.fpdf.org/

寻找手动菜单

说明

将文档发送到给定的目的地:浏览器、文件或字符串。在浏览器的情况下,可以使用 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.

【讨论】:

  • 我添加到我的答案中。您应该查看我链接到的站点以了解该库的工作原理。完成文档创建后添加代码
  • 也许@joelgoldstick 的意思是,将您的$pdf-&gt;output(); 更改为$pdf-&gt;Output('your file name.pdf', 'I'); CMIIW
猜你喜欢
  • 2012-11-29
  • 2018-01-07
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多