2017-01-25 5 views
0

Ich habe ein HTML-Formular, das die Antworten auf ein PDF-Dokument sendet. Alles funktioniert gut, aber ich möchte, dass die Fragen auch auf dem PDF-Dokument stehen.FPDF einfügen Text

zur Zeit sieht es so auf dem pdf:

Jurgen 
Yes 
No 
4 
No 

Ich würde es gerne sein:

Name: Jurgen 
Do you own a vehicle? Yes 
etc 

(GELÖST) meine aktuellen Code für die fpdf Datei:

<?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(); 

Ich lerne immer noch über FPDF, da ihre Dokumentation nicht die beste ist. Wenn ich Fehler gemacht habe, lass es mich wissen. Danke

+0

Beitrag aussehen sollte Ihre volles HTML Formular, also kann ich y geben ou eine richtige Antwort, ich habe fpdf viel verwendet –

+0

@MasivuyeCokile Ich habe dieses Problem thx AnthonyB sortiert. Vielen Dank, Sir, aber ich habe Probleme beim Einstellen eines Seitenkopfes. Wenn Sie mir ein Beispiel dafür geben könnten? – RedZ

+0

per Seitenkopf Sie meinen die Kopfzeile auf Ihrer PDF-Datei? –

Antwort

0

Um dies zu tun, können Sie Ihre Frage in einem assoziativen Array haben, wo der Schlüssel mit name Attribut in Ihrem HTML-Formular übereinstimmt.

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

bearbeiten Wie würde ich dies auf meinem HTML-Formular Code Sir? Derzeit habe es als Name: RedZ

+0

Danke sir im ganzen set ^^ – RedZ

0

kann ich Ihnen nicht mit dieser expliziten Frage helfen, aber ich hatte vor ein Skript ein paar Tage zu schreiben, die auch schafft ein pdf. Ich fand ein sehr nettes Werkzeug, das ich anstelle von fpdf verwenden wollte. Es konvertiert HTML-Code direkt in eine PDF-Datei. (Sie können auch css einbinden) Vielleicht kann das auch für Sie interessant sein. https://github.com/spipu/html2pdf

+0

In der Tat html2pdf ist ein großartiges Werkzeug, um direkt eine HTML-Ansicht in PDF zu konvertieren. Es ist ein einfacher Weg, es zu tun. Aber hier versucht er, das PDF selbst zu erstellen, und das Problem mit der Frage bleibt gleich. – AnthonyB

0

Dies sollte tun

<?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(); 
?> 

Header hinzugefügt, um das Problem vor Ihnen jetzt

<?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'); 
    } 
} 
?> 

-Update Dies ist, wie Sie Ihre endgültige Code

<?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

Füge ich das nur über meinen aktuellen Code hinzu? – RedZ

+0

über Ihrem aktuellen Code –

+0

Geben Sie mir einen Fehler, Sir. – RedZ