我有一个html表单,将答案发送到pdf文档。一切都运转正常,但我也想在pdf文档中提出问题。
目前在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 :(得分: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();
答案 1 :(得分:0)
我无法帮助你解决这个明确的问题,但我必须在几天前编写一个脚本,这也会创建一个pdf。 我发现了一个非常好的工具,我决定使用而不是fpdf。它直接将html代码转换为pdf。 (你也可以将css链接到它)也许这对你来说也很有趣。 https://github.com/spipu/html2pdf
答案 2 :(得分: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();