【问题标题】:How to set the height of table in mpdf如何在mpdf中设置表格的高度
【发布时间】:2020-09-25 13:44:49
【问题描述】:

我正在使用 MPDF 生成发票。我的项目行是动态的,因此显示项目的表格的高度应该是灵活的。当我使用以下代码打印 PDF 时:

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

它按如下方式打印 pdf:

但我需要按如下方式打印:

我尝试过使用 line-height,但它会扭曲视图,因为 td 中的 valign 不会对齐顶部的内容。

line-height:100px 在 TD 打印中如下所示:

有没有办法通过动态增加项目表的高度来填充整个页面。

【问题讨论】:

  • 嘿兄弟,你能解决这个问题吗?

标签: php html mpdf


【解决方案1】:

这是我使用过的一些解决方案。

  1. 计数tbody中的所有tr
  2. 然后使用 css set line-height of td 找到最后一个 tr tbody

【讨论】:

    【解决方案2】:

    我正在搜索 mpdf line-height 并找到了这个未回答的问题。

    这是我使用过的一些解决方案。

    选项 1

    1. 在循环期间计算每一行
    2. 估计每行的大致高度。通常这是固定在一行,所以这不是问题。
    3. 从所需的表格高度中减去(行 x 高度)
    4. 在循环期间,如果行数等于总行数,则它是最后一行,在这种情况下,您可以为最后一行添加 &lt;td height="##"&gt;

    选项 2

    添加填充行

    1. 计算发票上需要多少行才能填写正确的金额(保持小于最大值)
    2. 然后使用类似下面的东西填满表格

      if($current_row < $total_rows){ for($i=$current_row; $i<$total_rows; $i++){ echo '<tr><td colspan="4"></td></tr>'; } } ?>

    【讨论】:

      【解决方案3】:

      根据mPDF库here的文档,我们只能设置块级元素的高度。我遇到了同样的问题,然后我将我的表格移到了一个 div 中,并为 div 添加了高度,如下所示。

      <div height="870" style="border:solid 1px #ccc">
          <table class="table>
               Your table content here...
          </table>
      </div>
      

      【讨论】:

        【解决方案4】:

        @jecorrales 问我是如何解决我的问题的。

        我使用简单的 window.print() 打印了发票,但使用 css 设置如下:

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

        其中$invoice 包含数据数组。

        我正在定义静态编号。单个 A4 页面可以支持的行数。并且在这种情况下使用这个解决方案超过 2 年。

        查看最终结果https://screenshots.firefox.com/GRMfejUAEVjyiVpb/localhost

        【讨论】:

          【解决方案5】:

          您必须在单元格内指定它,如下所示:

          <td  style="height:4.6cm; vertical-align: text-top;"></td>
          

          【讨论】:

          • 第二行问题失败。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-23
          • 2017-12-27
          • 1970-01-01
          • 2012-11-20
          • 1970-01-01
          相关资源
          最近更新 更多