2016-02-26 47 views
1

我有一個表格,我想轉換爲pdf。我想用FPDF做 我的HTML代碼如下:運行php腳本將html轉換爲pdf

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

的文件是在XAMPP文件htdoc文件夾演示這樣的:

enter image description here

當我運行page.html即可(請點擊註冊):

enter image description here

這表明我:

enter image description here

爲什麼不產生一個PDF?

+0

是您的註冊文件'.php'擴展名或'.html'? – andre3wap

+0

什麼是註冊文件?下面的代碼是我所編碼的 – compcrk

+0

好的。我看到了編輯。 – andre3wap

回答

0

您需要輸出到一個文件:

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. 
+0

我在哪裏添加這段代碼? – compcrk

+0

我添加到我的答案。你應該看看我鏈接到的網站,瞭解這個圖書館的工作原理。在完成文檔的創建之後添加代碼 –

+0

或許@joelgoldstick的意思是,將你的'$ pdf-> output();'改成'$ pdf-> Output('your file name.pdf','I' );'CMIIW –