如果 DOM 中的名称属性数量未知,如何使用 PHP 使用 FPDF 创建新单元格?
How do I create new cells with FPDF using PHP if the number of name attributes in the DOM is unknown?
我正在开发一个发票,当用户点击“添加”按钮生成一个新字段时,它可以生成新的输入字段。然后在文档的末尾,我制作了一个 "GET PDF" 按钮,您可以在准备好后获得发票的 PDF 副本。为了实现这一点,我一直在使用 FPDF 库,到目前为止,应用程序运行良好,除了一件事。 PDF 文档基本上必须单独构建,我找不到让 PHP 查看我正在构建应用程序的文件并检索具有所有值的名称属性以便注入它们的好方法在我在不同文件中制作的 FPDF 模板中。我是 PHP 的新人,我不知道该怎么做。包含 Invoice App 的文件名为 index.php,包含 PDF 模板的文件位于 pdf.php
in index.php:
<form action="pdf.php" method="post">
<div class="inputForms">
<div id="input_fields" class="input_fields_wrap">
<div class="new"><input name="description" class="description" type="text" maxlength="255" placeholder="Enter Description" value=""/>
<input name="r" class="rate qty" type="text" maxlength="255" placeholder="0" size="5" value=""/>
<input name="p" class="pack price" type="text" maxlength="255" placeholder="$ 0.00" size="5" value=""/>
<input id="amount" class="amount" name="amount" type="text"></div>
</div>
</div>
</form>
in pdf.php:
<?php
$invoiceNumber = $_POST['InvoiceNumber'];
$clientName = $_POST['clientName'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$insurance_name = $_POST['insurance_name'];
$claim = $_POST['claim'];
$description = $_POST['description'];
$r = $_POST['r'];
$p = $_POST['p'];
$amount = $_POST['amount'];
$totalAmount = $_POST['total_amount'];
$afterTax = $_POST['after_tax'];
$notes = $_POST['notes'];
require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","",12);
$pdf->Cell(52);
$pdf->Cell(50,10,"The Company Name",1,0,"R");
$pdf->Cell(0,10,"INVOICE",1,1,"C");
$pdf->Cell(52);
$pdf->Cell(50,10,"City, State, Zip",1,0,"R");
$pdf->Cell(30,10,"Date:",1,0,"R");
$pdf->Cell(0,10,"04-15-2016",1,1,"L");
$pdf->Cell(52);
$pdf->Cell(50,10,"Phone: (555)777-7777",1,0,"R");
$pdf->Cell(30,10,"Time:",1,0,"R");
$pdf->Cell(0,10,"15 : 21 PM",1,1,"L");
$pdf->Cell(52);
$pdf->Cell(50,10,"Fax: (555)777-7777",1,0,"R");
$pdf->Cell(30,10,"#",1,0,"R");
$pdf->Cell(0,10,"{$invoiceNumber}",1,1,"L");
$pdf->Cell(52);
$pdf->Cell(0,10,"E-mail: something@example.com",1,1,"L");
$pdf->Image('images/hlogo.jpg',10,6,50);
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(90,10,"Bill To",1,0,"L");
$pdf->Cell(0,10,"Insurance Information",1,1,"L");
$pdf->Cell(90,10,"{$clientName}",1,0,"L");
$pdf->Cell(0,10,"{$insurance_name}",1,1,"L");
$pdf->Cell(90,10,"{$address}",1,0,"L");
$pdf->Cell(0,10,"{$claim}",1,1,"L");
$pdf->Cell(90,10,"{$phone}",1,0,"L");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(100,10,"item",1,0,"L");
$pdf->Cell(25,10,"Quantity",1,0,"C");
$pdf->Cell(25,10,"Rate",1,0,"C");
$pdf->Cell(0,10,"Amount",1,1,"C");
$pdf->Cell(100,10,"{$description}",1,0,"L");
$pdf->Cell(25,10,"{$r}",1,0,"C");
$pdf->Cell(25,10,"$ {$p}",1,0,"L");
$pdf->Cell(0,10,"$ {$amount}",1,1,"L");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(150,10,"SubTotal",1,0,"R");
$pdf->Cell(0,10,"$ {$totalAmount}",1,1,"L");
$pdf->Cell(150,10,"Tax",1,0,"R");
$pdf->Cell(0,10,"% 6",1,1,"L");
$pdf->Cell(150,10,"Total",1,0,"R");
$pdf->Cell(0,10,"$ {$afterTax}",1,1,"L");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(0,10,"Notes",1,1,"L");
$pdf->Cell(140,10,"{$notes}",1,1,"L");
$pdf->output();
?>
我会用 "field_01"、"field_02" 等模式命名新字段。然后,您可以像这样遍历创建的 POST 字段:
foreach ($_POST as $key=>$value){
if (substr($key,0,6) === "field_"){
$pdf->Cell(140,10,"$value",1,1,"L");
}
}
这是一个简单的方法:
通过使用循环:
注意: 你总是可以多次循环执行此操作,但你可以像这样使其动态化:
<?php
//Assuming that $data holds all the elements of your specific name attribute
$data = $_POST;
for ($i=0;$i <= count($data);$i++) {
$pdf->Cell($i); //Here you can do whatever you want with your cell
}
?>
注意: 因为 $_POST
包含所有输入字段值,如果您在表单中使用 POST 方法,所以它会计算所有这些输入字段然后它将根据字段数创建单元格..!
我正在开发一个发票,当用户点击“添加”按钮生成一个新字段时,它可以生成新的输入字段。然后在文档的末尾,我制作了一个 "GET PDF" 按钮,您可以在准备好后获得发票的 PDF 副本。为了实现这一点,我一直在使用 FPDF 库,到目前为止,应用程序运行良好,除了一件事。 PDF 文档基本上必须单独构建,我找不到让 PHP 查看我正在构建应用程序的文件并检索具有所有值的名称属性以便注入它们的好方法在我在不同文件中制作的 FPDF 模板中。我是 PHP 的新人,我不知道该怎么做。包含 Invoice App 的文件名为 index.php,包含 PDF 模板的文件位于 pdf.php
in index.php:
<form action="pdf.php" method="post">
<div class="inputForms">
<div id="input_fields" class="input_fields_wrap">
<div class="new"><input name="description" class="description" type="text" maxlength="255" placeholder="Enter Description" value=""/>
<input name="r" class="rate qty" type="text" maxlength="255" placeholder="0" size="5" value=""/>
<input name="p" class="pack price" type="text" maxlength="255" placeholder="$ 0.00" size="5" value=""/>
<input id="amount" class="amount" name="amount" type="text"></div>
</div>
</div>
</form>
in pdf.php:
<?php
$invoiceNumber = $_POST['InvoiceNumber'];
$clientName = $_POST['clientName'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$insurance_name = $_POST['insurance_name'];
$claim = $_POST['claim'];
$description = $_POST['description'];
$r = $_POST['r'];
$p = $_POST['p'];
$amount = $_POST['amount'];
$totalAmount = $_POST['total_amount'];
$afterTax = $_POST['after_tax'];
$notes = $_POST['notes'];
require("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial","",12);
$pdf->Cell(52);
$pdf->Cell(50,10,"The Company Name",1,0,"R");
$pdf->Cell(0,10,"INVOICE",1,1,"C");
$pdf->Cell(52);
$pdf->Cell(50,10,"City, State, Zip",1,0,"R");
$pdf->Cell(30,10,"Date:",1,0,"R");
$pdf->Cell(0,10,"04-15-2016",1,1,"L");
$pdf->Cell(52);
$pdf->Cell(50,10,"Phone: (555)777-7777",1,0,"R");
$pdf->Cell(30,10,"Time:",1,0,"R");
$pdf->Cell(0,10,"15 : 21 PM",1,1,"L");
$pdf->Cell(52);
$pdf->Cell(50,10,"Fax: (555)777-7777",1,0,"R");
$pdf->Cell(30,10,"#",1,0,"R");
$pdf->Cell(0,10,"{$invoiceNumber}",1,1,"L");
$pdf->Cell(52);
$pdf->Cell(0,10,"E-mail: something@example.com",1,1,"L");
$pdf->Image('images/hlogo.jpg',10,6,50);
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(90,10,"Bill To",1,0,"L");
$pdf->Cell(0,10,"Insurance Information",1,1,"L");
$pdf->Cell(90,10,"{$clientName}",1,0,"L");
$pdf->Cell(0,10,"{$insurance_name}",1,1,"L");
$pdf->Cell(90,10,"{$address}",1,0,"L");
$pdf->Cell(0,10,"{$claim}",1,1,"L");
$pdf->Cell(90,10,"{$phone}",1,0,"L");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(100,10,"item",1,0,"L");
$pdf->Cell(25,10,"Quantity",1,0,"C");
$pdf->Cell(25,10,"Rate",1,0,"C");
$pdf->Cell(0,10,"Amount",1,1,"C");
$pdf->Cell(100,10,"{$description}",1,0,"L");
$pdf->Cell(25,10,"{$r}",1,0,"C");
$pdf->Cell(25,10,"$ {$p}",1,0,"L");
$pdf->Cell(0,10,"$ {$amount}",1,1,"L");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(150,10,"SubTotal",1,0,"R");
$pdf->Cell(0,10,"$ {$totalAmount}",1,1,"L");
$pdf->Cell(150,10,"Tax",1,0,"R");
$pdf->Cell(0,10,"% 6",1,1,"L");
$pdf->Cell(150,10,"Total",1,0,"R");
$pdf->Cell(0,10,"$ {$afterTax}",1,1,"L");
$pdf->Cell(0,10,"",1,1,"C");
$pdf->Cell(0,10,"Notes",1,1,"L");
$pdf->Cell(140,10,"{$notes}",1,1,"L");
$pdf->output();
?>
我会用 "field_01"、"field_02" 等模式命名新字段。然后,您可以像这样遍历创建的 POST 字段:
foreach ($_POST as $key=>$value){
if (substr($key,0,6) === "field_"){
$pdf->Cell(140,10,"$value",1,1,"L");
}
}
这是一个简单的方法:
通过使用循环:
注意: 你总是可以多次循环执行此操作,但你可以像这样使其动态化:
<?php
//Assuming that $data holds all the elements of your specific name attribute
$data = $_POST;
for ($i=0;$i <= count($data);$i++) {
$pdf->Cell($i); //Here you can do whatever you want with your cell
}
?>
注意: 因为 $_POST
包含所有输入字段值,如果您在表单中使用 POST 方法,所以它会计算所有这些输入字段然后它将根据字段数创建单元格..!