duanjia8215 2016-04-25 08:40
浏览 379
已采纳

如何在mpdf中设置表的高度

I am generating invoices using MPDF. My item rows are dynamic so the height of table which shows items should be flexible. When I am printing the PDF using following code:

    <!-- Latest compiled and minified CSS -->   
<?php echo $this->Html->css(array('bootstrap.min','AdminLTE.min','skin-green','font-awesome.min')); ?>
<style type="text/css">
  .products{
    width: 100%;
    border-collapse: collapse;
  }
  h2,h3{
    margin: 0;
    padding:0;
  }
  .border{
    border: 1px solid black;
    padding:5px;
  }
  table.products{
    border: 1px solid black;
    height: 500px;
    margin-left: -6px;
    margin-right: -6px;
    margin-bottom: -6px;
  }
  .products tr td{
    padding:5px;
    border:1px solid #333;
  }
  .products tr th{
    padding:5px;
    border:1px solid #333;
  }
  .pull-right{
    float: right;
  }
</style>
<!-- title row -->
<div class="border">
  <table width="100%">
    <tr>
      <td style="width:35%">

      </td>
      <td style="width:35%" align="center">
        Estimate
      </td>
      <td style="width:35%" align="right">
        Date: <?php echo  date('d-m-Y',strtotime($invoice['Invoice']['dated'])); ?>
      </td>
    </tr>
    <tr>
      <td colspan="3" align="center">
        <br>
        <h3>Invoice Heading</h3>

      </td>                
    </tr>
  </table>
  <hr/>
  <div class="row">
    <div class="col-xs-12 border-right">
      To: <?php echo $invoice['Invoice']['user']; ?><br>
      Mobile.: <?php echo $invoice['Invoice']['mobile']; ?><br>
      Address: <?php echo $invoice['Invoice']['address']; ?><br>

    </div>    
  </div>
  <hr/> 
  <!-- Table row -->
  <div class="row">
    <div class="col-xs-12 table-responsive">
      <table class="products">
        <thead>
          <tr>              
            <th width="60"><?php echo __('Sr. No.'); ?></th>
            <th><?php echo __('Quantity'); ?></th>
            <th width="250"><?php echo __('Particulars'); ?></th>
            <th><?php echo __('Rate'); ?></th>
            <th><?php echo __('Amount'); ?></th>
          </tr>
        </thead>
        <tbody>
          <?php $i = 0; foreach ($invoice['InvoiceDetail'] as $invoiceDetail): $i++; ?>
            <tr>
              <td><?php echo $i; ?></td>
              <td><?php echo $invoiceDetail['quantity']; ?></td>
              <td><?php echo $invoiceDetail['Item']['name']; ?></td>
              <td><?php echo $invoiceDetail['price']; ?></td>
              <td align="right"><?php echo $invoiceDetail['amount'] ?></td>
            </tr>
          <?php endforeach; ?>
            <tr> 
              <td colspan="3"></td>             
              <td>Total: </td>
              <td align="right"><i class="fa fa-inr"></i> <?php echo number_format((float)$invoice['Invoice']['total'], 2, '.', ''); ?>
              </td>
            </tr>  
            <tr>
              <td colspan="5">Amount in words: Rs.<?php echo $c2w ?></td>
            </tr>
        </tbody>                  
      </table>
    </div><!-- /.col -->
  </div><!-- /.row -->
</div>          

It prints the pdf as follows: enter image description here

But I need to print it as follows:

enter image description here

I have tried using line-height but it distorts the view because valign in td will not align the content at top.

With line-height:100px in TD print looks like follows:

enter image description here

Is there a way to fill the complete page by increasing the height of items table dynamically.

  • 写回答

5条回答 默认 最新

  • douba4933 2018-10-11 01:51
    关注

    As @jecorrales asked how I solved my problem.

    I have printed the invoice using simple window.print() but setting it using css as follows:

        <style type="text/css">
      @page {
          size: 21cm 29.7cm;
          margin: 0; /* change the margins as you want them to be. */
      }
    
      body  
      {
          /* this affects the margin on the content before sending to printer */ 
          margin: 0px;  
      } 
      .row1{
        margin-right: -12px;
        margin-left: 0px;
      }
      .products{
        width: 100%;
        border-collapse: collapse;
      }
      h2,h3,h4,h5{
        margin: 0;
        padding:0;
      }
      hr{
        margin-bottom: 5px;
        margin-top: 5px;
      }
      .border{
        border: 1px solid black;
        padding:5px;
      }
      table.products{
        border: 1px solid black;
        margin-left: -6px;
        margin-right: -6px;
        margin-bottom: -6px;
      }
      .products tr td{
        padding:3px;
        border:1px solid #333;
      }
      .products tr th{
        padding:3px;
        border:1px solid #333;
      }
      .pull-right{
        float: right;
      }
      .no-margin{
        margin: 0;
      }
      .no-b{
        border-top:0px !important;
        border-bottom: 0px !important;
      }
      .logo{
        position: absolute;
        width: 90px;
        left: 20px;
        top: 50px;
      }
    </style>
    <!-- title row -->
    <div class="col-md-12 border no-margin">
      <table class="col-md-12">
        <tr>
          <td style="width: 400px;" valign="top">
            TIN No. 00000000000000 Dated: 01-04-2005<br>
            C.S.T. No. 0000000000 Dated: 11-06-2001
          </td>
          <td style="width: 400px;" align="center" valign="top">
            <u>Retail Invoice</u>
          </td>
          <td style="width: 400px;" align="right" valign="top">
            Mobile: 00000000000<br>
            Phone: 0000000000000<br>
            Tele Fax: 00000000000000<br>
            Email: email
          </td>
        </tr>
        <tr>
          <td colspan="3" align="center">
            <img class="logo" src="../../images/logo.jpg">
            <br>
            <h3>Company Name</h3>
            <h4>Deals in: Products</h4>
            <h5>Address here</h5>
          </td>                
        </tr>
      </table>
      <hr>
      <br>
      <div class="row">
        <div class="col-xs-7 border-right">
          To: <?php echo $invoice['Invoice']['user']; ?><br>
          Address: <?php echo $invoice['Invoice']['address']; ?><br>
          Registration No.: <?php echo $invoice['Invoice']['reg_no']; ?><br>
        </div>
        <div class="col-xs-5">
          Bill No. <?php echo $invoice['Invoice']['id']; ?><br>
          Dated: <?php echo date('d-m-Y',strtotime($invoice['Invoice']['dated'])); ?><br>
        </div>
      </div>
      <hr>
      <table style="width: 100%">
        <tr>
          <td>Name & Address of Transport Co. </td><td><?php echo $invoice['Invoice']['transport']; ?></td>
        </tr>
        <tr>
          <td>GRR: <?php echo $invoice['Invoice']['grr']; ?></td>
          <td>Vehicle No.</td><td><?php $invoice['Invoice']['vehicle_no']; ?></td>
        </tr>
      </table>
      <hr>
      <!-- Table row -->
      <div class="row1">
          <table class="products">
            <thead>
              <tr height="20">              
                <th width="60"><?php echo __('Sr. No.'); ?></th>
                <th><?php echo __('Particulars'); ?></th>
                <th width="80"><?php echo __('Quantity'); ?></th>
                <th width="80"><?php echo __('Price'); ?></th>
                <th width="80"><?php echo __('Amount'); ?></th>
                <th width="80"><?php echo __('Tax'); ?></th>
                <th width="80"><?php echo __('Unit'); ?></th>
                <th width="80"><?php echo __('Item Total'); ?></th>
              </tr>
            </thead>
            <tbody>
              <?php $i = 0; foreach ($invoice['InvoiceDetail'] as $invoiceDetail): $i++; ?>
                <tr height="20">
                  <td><?php echo $i; ?></td>
                  <td><?php echo $invoiceDetail['Item']['name']; ?></td>
                  <td><?php echo $invoiceDetail['quantity']; ?></td>
                  <td><?php echo $invoiceDetail['price']; ?></td>
                  <td><?php echo $invoiceDetail['amount']; ?></td>
                  <td><?php echo $invoiceDetail['tax']; ?></td>
                  <td><?php echo $invoiceDetail['unit']; ?></td>
                  <td align="right"><?php echo $invoiceDetail['item_total'] ?></td>
                </tr>
              <?php endforeach; ?>
              <?php for ($i=count($invoice['InvoiceDetail']); $i <=16  ; $i++) {  ?>
                <tr class="no-b" height="26">
                  <td class="no-b"></td>
                  <td class="no-b"></td>
                  <td class="no-b"></td>
                  <td class="no-b"></td>
                  <td class="no-b"></td>
                  <td class="no-b"></td>              
                  <td class="no-b"></td>
                  <td class="no-b"></td>
                </tr>
              <?php } ?>
    
                <tr height="20">
                  <td id="getrow" colspan="6" rowspan="4" align="top">
                    Amount in words: Rs.<?php echo $c2w ?></td>
                  <td height="30">Total: </td>
                  <td align="right"><i class="fa fa-inr"></i> <?php echo number_format((float)$invoice['Invoice']['subtotal'], 2, '.', ''); ?>
                  </td>
                </tr>
                <tr id="dynTable" height="20">
                  <td>Adjustment:</td>
                  <td align="right"><i class="fa fa-inr"></i> <?php echo $invoice['Invoice']['adjustment']; ?></td>
                </tr>
                <tr height="20">
                  <td>Grand Total:</td>
                  <td align="right"><i class="fa fa-inr"></i> <?php echo number_format((float)round($invoice['Invoice']['total']), 2, '.', ''); ?></td>
                </tr>
                <tr height="20">
                  <td colspan="8">
                    <h5>I AM LIABLE TO PAY TAX ON THE VALUE ABOVE AND AUTHORISED TO SIGN THE INVOICE</h5>
                  </td>
                </tr>
                <tr>
                  <td style="height: 20px;" valign="top" colspan="6">
                    <ol>
                      <li>Goods once sold cannot be taken back.</li>
                      <li>Subject to Malerkolta Jurisdiction only.</li>
                      <li>All bills standing unpaid after 15 days from presentation an interest will be charged @ 24%</li>
                    </ol>
                    E.&.O.E.
                  </td>
                  <td valign="top" colspan="2" align="right">
                    For Company Name
                    <br><br><br>
                    Auth. Signatory
                  </td>
                </tr>
            </tbody>                  
          </table>
      </div><!-- /.row -->
    </div>   
    

    In it $invoice is containing array of data.

    And I am defining static no. of rows which can be supported by single A4 page. And using this solution for more than 2 years for this situation.

    See final result https://screenshots.firefox.com/GRMfejUAEVjyiVpb/localhost

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色