2017-01-25 3 views
0

У меня есть html-форма, которая отправляет ответы на pdf-документ. Все работает нормально, но я хотел бы, чтобы вопросы были и в документе PDF.FPDF insert text

В настоящее время это выглядит на 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 Я отсортировал эту проблему вне tox до AnthonyB. Спасибо, сэр, но у меня проблемы с настройкой заголовка страницы. Если бы вы могли привести мне пример? – RedZ

+0

по заголовку страницы вы имеете в виду заголовок в вашем файле pdf? –

ответ

0

Для этого вы можете задать свой вопрос в ассоциативном массиве, где ключ соответствует атрибуту name в вашей форме html.

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

Спасибо, sir im all set ^^ – 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'); 
    } 
} 
?> 

Update это как ваш окончательный код должен выглядеть

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

Смежные вопросы