create dynamic invoice bill using mpdf

帅比萌擦擦* 提交于 2019-12-08 06:01:29

问题


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

please i am already embeded mpdf lib in a website.
Now i want to generate dynamic pdf's for invoice .
how to build dynamic table to $html variable? then i should pass it to WriteHTML()

 $mpdf->WriteHTML($html);

Then i will call $mpdf->Output('downloads/application.pdf','F'); to download pdf

SQL PART

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

PHP PART

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

i am using mysql


回答1:


I just have my cellphone to write this, so the code may be no perfect.

Use a foreach loop to iterate over your query results to construct a $htmlRows string. You didn't show us your php command to query and the result variable. So I assume $rows as an array of records.

$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>
    ";
}

Do this loop before you generate $html.

Then when you assing the code to $html just replace all the non dynamic rows by

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


来源:https://stackoverflow.com/questions/25589922/create-dynamic-invoice-bill-using-mpdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!