使用FPDF在所有页面中添加子标题 [英] Add sub-headers in all pages with FPDF

查看:121
本文介绍了使用FPDF在所有页面中添加子标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成PDF文件的简单脚本.我从数据库中获取所需的信息,并使用foreach来创建PDF文件的页面,最后一页中有一些图表和其他信息未从数据库中检索出来.除最后一页外,所有页面都必须有一个子标题.

I have a simple script that generates a PDF file. I'm getting the info I need from the database and using a foreach to create the pages of the PDF file, and the last page I have some charts and other info that are not retrived from DB. All the pages, except this last one, must have a subheader.

我当前的代码是:

foreach ($sql as $key => $value) 
{
    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['value'], '', '', 'L');
}

基本上,我需要有描述每行的子标题.但是,如果执行此操作,则所有行都将带有子标题:

Basically, I need to have the subheader describing each row. But If I do this, all the lines will have the subheader:

foreach ($sql as $key => $value) 
{
    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
    $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');

    // 

    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

除最后一页外,每页中都需要此子标题.我已经尝试使用pageNo()函数编写一些疯狂的代码,但是没有用.

I need this subheader in each page, except the last one. I've tried to do some crazy code with pageNo() function, but it didn't work.

谢谢!

推荐答案

覆盖类中 FPDF 的方法 header().

class PDF extends FPDF
{
    public function Header() {
        $this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R');
    }
}

添加新页面时,FPDF会自动将该方法称为 .

This method will be called automatically by the FPDF while adding a new page.

这篇关于使用FPDF在所有页面中添加子标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆