【问题标题】:create dynamic invoice bill using mpdf使用 mpdf 创建动态发票账单
【发布时间】:2014-10-24 17:50:10
【问题描述】:

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

请我已经在网站中嵌入了 mpdf lib。
现在我想为发票生成动态 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

【问题讨论】:

  • 动态是什么意思?您使用用户填写的表单而不是呈现 pdf 或某种数据库结构?
  • 我必须从数据库中建表。
  • 对 db 进行查询,并将它们打印到 $html 变量中。顺便说一句,你的问题不清楚。我们无法为您提供更少的细节。
  • 你想要什么样的详细信息我会在这里发布,请告诉我如何在 html 变量中构造表格
  • 显示您的 php 端和 sql 查询以打印成 pdf。

标签: php pdf-generation tcpdf fpdf mpdf


【解决方案1】:

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

使用 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>...."
;

【讨论】:

  • 我应该像这样添加对吗? $html = " .... ....." 。 $html 行。 "...." ;
  • 当然 .... 只是您在询问之前已经拥有的内容的占位符。希望您现在可以自己解决。
猜你喜欢
相关资源
最近更新 更多
热门标签