如果DOM中的名称属性数量未知,如何使用PHP使用FPDF创建新单元格?

时间:2016-05-04 23:24:40

标签: php fpdf

我开发的发票可以在用户点击“添加”按钮生成新字段时生成新的输入字段。然后在文档的最后我做了一个" GET PDF"一旦准备就绪,您可以使用该按钮获取发票的PDF副本。为了实现这一点,我一直在使用FPDF库,到目前为止,应用程序运行良好,除了一件事。 PDF文档必须基本上单独构建,我找不到一个好方法让PHP查看我构建应用程序的文件,并检索所有值的名称属性,以便注入它们在我在不同文件中制作的FPDF模板中。我是PHP的新手,我不知道如何解决这个问题。包含发票应用程序的文件名为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();

?>      

2 个答案:

答案 0 :(得分:0)

我会使用&#34; field_01&#34;,&#34; field_02&#34;等模式命名新字段。然后,您可以像这样循环创建的POST字段:

foreach ($_POST as $key=>$value){
if (substr($key,0,6) === "field_"){
        $pdf->Cell(140,10,"$value",1,1,"L");
    }
}

答案 1 :(得分:0)

这是一种简单的方法:

通过使用循环:

注意:您可以随时循环播放多次,但您可以通过以下方式使其动态化:

$_POST

注意:如果您在表单中使用POST方法,则... "SituacaoTributaria": [{ "value": 00, "situacao": "00 - Tributada Integralmente" }, { "value": 10, "situacao": "10 - Tributada com Cobrança de ICMS por ST" }, ... 包含所有输入字段值,因此它将计算所有这些输入字段,然后它将根据字段数量......!