You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次使用mPDF:如何实现PDF每页显示30条数据的分页功能

实现mPDF每页30条数据的分页显示方案

嘿,第一次用mPDF做分页是吧?我给你两种简单的实现方法,都能满足你每页30条数据的需求,看你喜欢哪种:

方法一:视图内直接计数插入分页标签(快速上手)

这种方法不用改太多控制器代码,直接在视图里循环计数,每30条数据后插入mPDF的分页标签就行,还能避免最后一页多出来空页。

修改你的视图代码如下:

<?php 
$i = 1;
$total_records = $rec->num_rows(); // 获取总记录数
foreach ($rec->result_array() as $key){ 
?>
<tr>
    <td class="centerthis"><?php echo $i; ?></td>
    <td class="centerthis"><?php $date = new DateTime($key['date_reg']); echo $date->format('m-d-Y');?></td>
    <td class="centerthis"><?php echo $key['lname'] ?></td>
    <td class="centerthis"><?php echo $key['fname'] ?></td>
    <td class="centerthis"><?php echo $key['mi'] ?></td>
    <td class="centerthis"><?php $date = new DateTime($key['bdate']); echo $date->format('m-d-Y'); ?></td>
    <td class="centerthis"><?php echo $key['amount'] ?></td>
</tr>
<?php 
// 每30条插入分页,最后一页不需要加
if ($i % 30 == 0 && $i != $total_records) {
    echo '<pagebreak />';
    // 如果每页需要重复显示表头,把表头的HTML代码放在这里就行
}
$i++;
} 
?>

小提示:

如果你的表头需要每页都显示,就在<pagebreak />后面重新输出表头的HTML代码,这样每一页顶部都会有表头。

方法二:控制器拆分数据,视图循环分页(更清晰)

这种方法先在控制器里把所有数据拆成每页30条的数组块,然后视图里循环每个块,处理起来逻辑更清晰,适合需要复杂分页布局的场景。

首先修改控制器代码:

public function print_rel(){ 
    $query_result = $this->home_model->get_latest_data();
    // 将查询结果拆分为每页30条的数组
    $all_records = $query_result->result_array();
    $data['page_records'] = array_chunk($all_records, 30);
    
    $html = $this->load->view('print_rel',$data, true); 
    $this->load->library('pdf'); 
    $pdf = $this->pdf->load(); 
    $pdf->AddPage('P', 
        '', '', '', '', 
        10, // margin_left 
        10, // margin right 
        40, // margin top 
        0, // margin bottom 
        14, // margin header 
        12); 
    $pdf->WriteHTML($html); 
    $pdf->Output(); 
}

然后修改视图代码(记得加上表头,每页都会显示):

<?php 
$page_num = 0;
foreach ($page_records as $page) {
    // 输出每页的表头
?>
<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Registration Date</th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>MI</th>
            <th>Birth Date</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
<?php 
    // 计算当前页的起始编号,保证编号连续
    $start_num = ($page_num * 30) + 1;
    $i = $start_num;
    foreach ($page as $key){ 
?>
    <tr>
        <td class="centerthis"><?php echo $i; ?></td>
        <td class="centerthis"><?php $date = new DateTime($key['date_reg']); echo $date->format('m-d-Y');?></td>
        <td class="centerthis"><?php echo $key['lname'] ?></td>
        <td class="centerthis"><?php echo $key['fname'] ?></td>
        <td class="centerthis"><?php echo $key['mi'] ?></td>
        <td class="centerthis"><?php $date = new DateTime($key['bdate']); echo $date->format('m-d-Y'); ?></td>
        <td class="centerthis"><?php echo $key['amount'] ?></td>
    </tr>
<?php 
        $i++;
    }
?>
    </tbody>
</table>
<?php 
    // 不是最后一页就加分页标签
    if ($page !== end($page_records)) {
        echo '<pagebreak />';
    }
    $page_num++;
}
?>

额外注意事项:

  • 确保你的mPDF版本支持<pagebreak />标签,新版本都没问题,如果遇到不生效的情况,可以试试在控制器里用$pdf->AddPage()方法,但HTML标签的方式更灵活。
  • 要是担心表格行被拆分到两页,可以给表格加CSS样式:table { page-break-inside: avoid; },避免内容跨页断裂。
  • 测试的时候可以先弄31条数据,看看分页是否正常,确保最后一页不会多出空页。

内容的提问来源于stack exchange,提问作者John Uy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:36:33