2017-01-25 83 views
0

我有一個將答案發送到pdf文檔的html表單。一切工作正常,但我希望問題也在PDF文檔中。FPDF插入文本

目前它看起來像這樣的PDF:

Jurgen 
Yes 
No 
4 
No 

我想它是:

Name: Jurgen 
Do you own a vehicle? Yes 
etc 

(解決) 我目前的FPDF文件代碼:

<?php 

class PDF extends FPDF 
{ 
// Page header 
function Header() 
{ 
    // Logo 
    $this->Image('images/logo.png', 10, 6, 30); 

    $this->SetFont('Arial', 'B', 15); 

    $this->Cell(50); 


    $this->Cell(90, 10, 'Document Title', 'C'); 
    // Line break 
    $this->Ln(20); 
} 

// Page footer 
function Footer() 
{ 

    $this->SetY(-15); 

    $this->SetFont('Arial', 'I', 8); 

    $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C'); 
} 
} 
?> 
<?php 
//set the question values 
$questions = array(
      'name' => "Name: ", 
      'date' => "Date: ", 
      'first' => "First Day of Leave: ", 
      'last' => "Last Day of Leave: ", 
      'days' => "Number of Days Taken: ", 
      'email' => "Managers Email: ", 
      'sig' => "Signed: ", 

     ); 
//set the question answers 
$date = $_POST['date']; 
$first = $_POST['first']; 
$last = $_POST['last']; 
$days = $_POST['days']; 
$email = $_POST['email']; 
$sig = $_POST['sig']; 
$name = $_POST['name']; 
//set the question names 
$questionName = $questions['name']; 
$questionDate = $questions['date']; 
$questionFirst = $questions['first']; 
$questionLast = $questions['last']; 
$questionDays = $questions['days']; 
$questionEmail = $questions['email']; 
$questionSig = $questions['sig']; 
//Create the PDF 

require('fpdf.php'); 

$pdf = new FPDF(); 
$pdf->AddPage(); 

$pdf->SetFont('Arial','B',16); 
//insert questions and answers 
$pdf->MultiCell(150,10,sprintf("%s %s", $questionDate, $date)); 
$pdf->Ln(); 
$pdf->MultiCell(150,10,sprintf("%s %s", $questionName, $name)); 
$pdf->Ln(); 
$pdf->MultiCell(150,10,sprintf("%s %s", $questionFirst, $first)); 
$pdf->Ln(); 
$pdf->MultiCell(150,10,sprintf("%s %s", $questionLast, $last)); 
$pdf->Ln(); 
$pdf->MultiCell(150,10,sprintf("%s %s", $questionDays, $days)); 
$pdf->Ln(); 
$pdf->MultiCell(150,10,sprintf("%s %s", $questionEmail, $email)); 
$pdf->Ln(); 
$pdf->MultiCell(50,10,sprintf("%s %s", $questionSig, $sig)); 
//display pdf 
$pdf->Output(); 

我仍然在學習FPDF,因爲他們的文檔不是最好的。如果我犯了一些錯誤,請告訴我。謝謝

+0

發表您的完整的HTML表單,所以我可以給y ou一個正確的答案,我已經使用fpdf很多 –

+0

@MasivuyeCokile我已經把這個問題排在了AnthonyB之外。謝謝先生,但我無法設置頁眉。如果你能給我一個例子嗎? – RedZ

+0

通過頁眉您的意思是在您的PDF文件的標題? –

回答

0

爲此,您可以在關聯數組中鍵入您的問題,該數組中的鍵與您的html表單中的name屬性匹配。

form.html

<form action="your_post.php" method="POST"> 
    <!-- Your first question about age --> 
    <label> 
     What's your name? 
     <input name="name" type="text" /> 
    </label> 
    <!-- Your second question --> 
    <label> 
     How old are you ? 
     <input name="yo" type="text" /> 
    </label> 
    <input type="submit" /> 
</form> 

your_post.php

<?php 
$questions = array(
       'name' => "What's your name?", 
       'yo' => 'How old are you?' 
      ); 
//Get you question and answer about name 
$name = $_POST['name']; 
$questionName = $questions['name']; 
//You can of course use a foreach through $_POST to get every question 

require('fpdf.php'); 

$pdf = new FPDF(); 
$pdf->AddPage(); 

$pdf->SetFont('Arial','B',16); 
//Here concatenate $questionName and $name 
$pdf->MultiCell(40,10,sprintf("%s %s", $questionName, $name)); 
$pdf->Output(); 
+0

以下我將如何編碼我的HTML格式先生?目前有它作爲名稱: RedZ

+0

謝謝先生我所有設置^^ – RedZ

0

我不能幫你明確的問題,但我不得不寫一個腳本前幾天,這也創造一個pdf。 我發現了一個很好的工具來做到這一點,我決定使用它來代替fpdf。它直接將html代碼轉換爲pdf。 (你也可以將css鏈接到它),也許這對你也很有趣。 https://github.com/spipu/html2pdf

+0

確實html2pdf是一個偉大的工具,直接將HTML視圖轉換爲PDF。這是一個簡單的方法來做到這一點。但是他在這裏試圖自己創建PDF,並且關於問題的問題保持不變。 – AnthonyB

0

這應該做

<?php 
$pdf = new PDF(); 

$pdf->Cell(20, 10, 'Name : ' . $name . ''); 
$pdf->Ln(); 
$pdf->cell(20, 10, 'Do you own a vehicle? : ' . $question1); 
$pdf->Ln(); 
$pdf->Output(); 
?> 

增加頭部,這個問題你現在面臨

<?php 

class PDF extends FPDF 
{ 
    // Page header 
    function Header() 
    { 
     // Logo 
     $this->Image('images/logo.png', 10, 6, 30); 

     $this->SetFont('Arial', 'B', 15); 

     $this->Cell(50); 


     $this->Cell(90, 10, 'Document Title', 'C'); 
     // Line break 
     $this->Ln(20); 
    } 

    // Page footer 
    function Footer() 
    { 

     $this->SetY(-15); 

     $this->SetFont('Arial', 'I', 8); 

     $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C'); 
    } 
} 
?> 

更新,這是你最後的代碼應該是什麼樣子

<?php 
//set the question values 
$questions  = array(
    'name' => "Name: ", 
    'date' => "Date: ", 
    'first' => "First Day of Leave: ", 
    'last' => "Last Day of Leave: ", 
    'days' => "Number of Days Taken: ", 
    'email' => "Managers Email: ", 
    'sig' => "Signed: " 

); 
//set the question answers 
$date   = $_POST['date']; 
$first   = $_POST['first']; 
$last   = $_POST['last']; 
$days   = $_POST['days']; 
$email   = $_POST['email']; 
$sig   = $_POST['sig']; 
$name   = $_POST['name']; 
//set the question names 
$questionName = $questions['name']; 
$questionDate = $questions['date']; 
$questionFirst = $questions['first']; 
$questionLast = $questions['last']; 
$questionDays = $questions['days']; 
$questionEmail = $questions['email']; 
$questionSig = $questions['sig']; 
//Create the PDF 

require('fpdf.php'); 

class PDF extends FPDF 
{ 
    // Page header 
    function Header() 
    { 
     // Logo 
     $this->Image('images/logo.png', 10, 6, 30); 

     $this->SetFont('Arial', 'B', 15); 

     $this->Cell(50); 


     $this->Cell(90, 10, 'Document Title', 'C'); 
     // Line break 
     $this->Ln(20); 
    } 

    // Page footer 
    function Footer() 
    { 

     $this->SetY(-15); 

     $this->SetFont('Arial', 'I', 8); 

     $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C'); 
    } 
} 


$pdf = new FPDF(); 
$pdf->AddPage(); 

$pdf->SetFont('Arial', 'B', 16); 
//insert questions and answers 
$pdf->MultiCell(150, 10, sprintf("%s %s", $questionDate, $date)); 
$pdf->Ln(); 
$pdf->MultiCell(150, 10, sprintf("%s %s", $questionName, $name)); 
$pdf->Ln(); 
$pdf->MultiCell(150, 10, sprintf("%s %s", $questionFirst, $first)); 
$pdf->Ln(); 
$pdf->MultiCell(150, 10, sprintf("%s %s", $questionLast, $last)); 
$pdf->Ln(); 
$pdf->MultiCell(150, 10, sprintf("%s %s", $questionDays, $days)); 
$pdf->Ln(); 
$pdf->MultiCell(150, 10, sprintf("%s %s", $questionEmail, $email)); 
$pdf->Ln(); 
$pdf->MultiCell(50, 10, sprintf("%s %s", $questionSig, $sig)); 
//display pdf 
$pdf->Output(); 
+0

我只是在我目前的代碼上面添加這個嗎? – RedZ

+0

高於您當前的代碼 –

+0

給我一個錯誤先生。 – RedZ