使用mpdf创建动态发票账单


create dynamic invoice bill using mpdf

MPDF

$html='
<body>
<div id="page">
  <div id="logo">
    <a href="http://www.danifer.com/"><img src="./HTML Invoice Template_files/invoice_logo.jpg"></a>
  </div><!--end logo-->
  <div id="address">
    <p><strong>'.$company.'</strong><br>
    <a href="mailto:'.$dbobj->getAdminEmail().'">'.$dbobj->getAdminEmail().'</a>
    <br><br>
    Transaction # xxx<br>
    Created on 2008-10-09<br>
    </p>
  </div><!--end address-->
  <div id="content">
    <p>
      <strong>Customer Details</strong><br>
      Name: '.$dbobj->UserFullName().'<br>
      Email: '.$dbobj->UserEmail().'<br>
      Contact: '.$dbobj->UserContact().'<br>
      Payment Type: MasterCard    </p>
    <hr>
    <table>
      <tbody>
        <tr>
        <td><strong>Description</strong></td>
        <td><strong>Qty</strong></td>
        <td><strong>Unit Price</strong></td>
        <td><strong>Amount</strong></td>
        </tr>
      <tr class="odd">
        <td>Product 1</td>
        <td>1</td>
         <td>Rs 1495.00</td>
        <td>Rs 1495.00</td>
      </tr>
      <tr class="even">
        <td>Product 2</td>
        <td>1</td>
       <td>Rs 1495.00</td>
        <td>Rs 1495.00</td>
      </tr>
        <tr class="odd">
          <td>Product 3</td>
          <td>1</td>
         <td>Rs 1495.00</td>
        <td>Rs 1495.00</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><strong>Total</strong></td>
          <td><strong>Rs 24485.00</strong></td>
        </tr>
    </tbody></table>

    <hr>
    <p>
      Thank you for your order.<br>
      If you have any questions, please feel free to contact us at <a href="mailto:'.$dbobj->getAdminEmail().'">'.$dbobj->getAdminEmail().'</a>.
    </p>
    <hr>
    <p>
      </p><center><small>This communication is for the exclusive use of the addressee and may contain proprietary, confidential or privileged information. If you are not the intended recipient any use, copying, disclosure, dissemination or distribution is strictly prohibited.
      <br><br>
      © '.$dbobj->sitename.' All Rights Reserved
      </small></center>
    <p></p>
  </div><!--end content-->
</div>
</body>;

请注意,我已经在一个网站中嵌入了mpdflib
现在我想为发票生成动态pdf
如何构建$html变量的动态表?然后我应该将它传递给WriteHTML()

 $mpdf->WriteHTML($html);

然后我将调用$mpdf->Output('downloads/application.pdf','F');下载pdf

SQL部分

select desc,qty,price,total from orders where productid=1

PHP部分

$mpdf=new mPDF();
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('downloads/application.pdf','F'); 

我正在使用mysql

我只有手机来写这个,所以代码可能并不完美。

使用foreach循环迭代查询结果以构造$htmlRows字符串。您没有向我们展示要查询的php命令和结果变量。所以我假设$rows是一个记录数组。

$htmlRows = "";
foreach($rows as $row) {
    $htmlRows .= "
        </tr>
        <tr class="even">
        <td>".$row->desc."</td>
        <td>".$row->qty."</td>
        <td>Rs ".$row->price."</td>
        <td>Rs ".$row->total."</td>
        </tr>
    ";
}

在生成$html之前执行此循环。

然后,当您将代码分配到$html时,只需用替换所有非动态行

$html = " ....
    .....</tr>" 
    . $htmlRows 
    . "<tr>...."
;