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

mPDF技术问题:如何在自动分页时调用指定函数?

问题描述

我现在用以下代码向PDF写入文本:

foreach($body as $html_body){
  $mpdf->WriteHTML('<div style="font-family: sakalbharati;"><p>'.$html_body.'</p></div>');
}

手动创建新页面时,我会调用report_header函数生成每页页眉,函数代码如下:

function report_header($mpdf,$page){ //This should be repeated in every page
    $mpdf->WriteHTML('<h1 style="text-align: center; color: rgb(50, 91, 205); font-size: 35px;">
    <u>REPORT HEADER</u></h1>');
    $mpdf->WriteHTML('<div style="text-align:center";><table style="background-color:rgb(81, 205, 50); border: 2px solid black;">
            <tr>
                <td style="border: 1px solid black;">
                    <img src="../logo.png" width="150" height="150">
                </td>
                <td style="border: 1px solid black;">
                    <img src="../image1.jpg" width="200" height="150">
                </td>
                <td style="border: 1px solid black;">
                    <img src="../image2.jpg" width="200" height="150">
                </td>
                <td style="border: 1px solid black; text-align:center">
                    <div style="background-color:rgb(148, 76, 17); width:100%; padding:5px; color: white; font-weight: bold;">'.trim($_POST["date"]).'</div><br>
                    <div style="background-color:rgb(20, 104, 104); padding:5px; color: white; font-style: italic; font-weight: bold;">News clips</div>
                </td>
            </tr>
            </table></div><br>');
    $mpdf->SetFooter("Page: ".$page);
    return $page;
}
// 手动分页时的调用方式
$mpdf->AddPage();
$page+=1;
$page = report_header($mpdf,$page);

但分页是自动触发的,我无法预知分页时机,请问怎么在自动分页时调用report_header函数?

解决方案

mPDF内置了页眉/页脚的自动回调机制,无需手动在分页时调用函数,按以下步骤实现即可:

  1. 调整页眉函数:让函数返回HTML内容,而非直接调用WriteHTML,适配mPDF的回调逻辑
  2. 注册回调与设置页眉:将函数绑定到mPDF的自动渲染流程中
  3. 统一页脚设置:利用mPDF的内置页码变量实现自动页脚

完整代码示例:

// 调整后的页眉函数,返回HTML内容,mPDF自动传入当前页码
function report_header($mpdf, $page) {
    $date = trim($_POST["date"]);
    return '<h1 style="text-align: center; color: rgb(50, 91, 205); font-size: 35px;">
    <u>REPORT HEADER</u></h1>
    <div style="text-align:center"><table style="background-color:rgb(81, 205, 50); border: 2px solid black;">
            <tr>
                <td style="border: 1px solid black;">
                    <img src="'.realpath('../logo.png').'" width="150" height="150">
                </td>
                <td style="border: 1px solid black;">
                    <img src="'.realpath('../image1.jpg').'" width="200" height="150">
                </td>
                <td style="border: 1px solid black;">
                    <img src="'.realpath('../image2.jpg').'" width="200" height="150">
                </td>
                <td style="border: 1px solid black; text-align:center">
                    <div style="background-color:rgb(148, 76, 17); width:100%; padding:5px; color: white; font-weight: bold;">'.$date.'</div><br>
                    <div style="background-color:rgb(20, 104, 104); padding:5px; color: white; font-style: italic; font-weight: bold;">News clips</div>
                </td>
            </tr>
            </table></div><br>';
}

// 初始化mPDF后,注册页眉回调
$mpdf->SetCallbacks([
    'report_header' => function($mpdf, $page) {
        return report_header($mpdf, $page);
    }
]);

// 设置页眉为回调函数,所有页面生效
$mpdf->SetHTMLHeader('{report_header}', '', true);

// 设置自动页脚,{PAGENO}为当前页码,{nb}为总页数
$mpdf->SetFooter('Page: {PAGENO}');

// 原有的文本写入逻辑保持不变
foreach($body as $html_body){
  $mpdf->WriteHTML('<div style="font-family: sakalbharati;"><p>'.$html_body.'</p></div>');
}

注意事项

  • 图片路径建议用realpath()转换为绝对路径,避免mPDF加载失败
  • 无需再手动调用report_header,mPDF会在自动分页、手动分页时自动渲染页眉
  • 若需区分奇偶页页眉,可在SetHTMLHeader中传入第二个参数:'O'表示仅奇数页,'E'表示仅偶数页

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 11:45:39