簡體   English   中英

運行PHP腳本將HTML轉換為PDF

[英]running php script to convert html to pdf

我有一個要轉換為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文件並單擊register時,它只是在php文件而不是生成pdf

文件位於文件夾演示中的XAMPP文件的htdoc中,如下所示:

在此輸入圖像描述

當我運行page.html(單擊注冊)時:

在此輸入圖像描述

它向我展示了這一點:

在此輸入圖像描述

為什么不生成pdf?

您需要輸出到文件:

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

將文檔發送到給定的目的地:瀏覽器,文件或字符串。 在瀏覽器的情況下,可以使用PDF查看器或強制進行下載。 必要時,該方法首先調用Close()以終止文檔。

參見http://www.fpdf.org/

查找“手動”菜單

描述

將文檔發送到給定的目的地:瀏覽器,文件或字符串。 在瀏覽器的情況下,可以使用PDF查看器或強制進行下載。 必要時,該方法首先調用Close()以終止文檔。 參數

dest將文檔發送到的目的地。 可以是以下之一:

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