FPDF 在每一个A4大小的页面的页脚处获得页码[英] FPDF Get page numbers at footer on Every A4 size page

本文是小编为大家收集整理的关于FPDF 在每一个A4大小的页面的页脚处获得页码的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用FPDF创建PDF报告.现在,如何在页面底部的报告的每个页面上生成页码. 以下是生成2页PDF的示例代码.

<?php
        require('fpdf.php');

        $pdf = new FPDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Arial','',16);

        $start_x=$pdf->GetX(); 
        $current_y = $pdf->GetY();
        $current_x = $pdf->GetX();

        $cell_width = 25; $cell_height=14; 
        $j = 20;  // This value will be coming from Database so we dont know how many pages the report is going to be 
        for ($i = 0; $i<$j ; $i++){
            $pdf->MultiCell($cell_width,$cell_height,'Hello1',1);
            $current_x+=$cell_width;
            $pdf->Ln();
        }

        $pdf->Output();


        ?>

注意:$ j值将来自数据库,因此我们不知道该报告将是多少页.

推荐答案

添加一个带有肖像方向的A4页面,请:

$pdf->AddPage("P","A4");

创建一个扩展FPDF类的新类,并覆盖预定义的Footer方法.

示例:

class PDF extends FPDF
{
    function Footer()
    {
        // Go to 1.5 cm from bottom
        $this->SetY(-15);
        // Select Arial italic 8
        $this->SetFont('Arial','I',8);
        // Print centered page number
        $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
    }
}

其他推荐答案

根据我的评论,您可以放置​​

$pdf->PageNo();

在您喜欢的任何地方.您也可以在此

中添加一个占位符
$pdf->AliasNbPages(); 

看起来像

$pdf->AliasNbPages('{totalPages}');

默认情况下是{nb}.不必添加占位符

您可以添加

的页面
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);

或没有您自己的占位符

$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{nb}", 0, 1);

这将产生例如

第1/10页

如果有10页:)

但是要谨慎

使用占位符会弄乱单元的宽度.因此,如果您有例如180的页面宽度大于90不再是中间(在您使用占位符的线路中).您会查看是否尝试:)

本文地址:https://www.itbaoku.cn/post/2090897.html

问题描述

I am creating PDF reports using FPDF. Now how do I generate page numbers on each page of a report at the bottom of the page. Below is the sample code for generating a 2 page PDF.

<?php
        require('fpdf.php');

        $pdf = new FPDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Arial','',16);

        $start_x=$pdf->GetX(); 
        $current_y = $pdf->GetY();
        $current_x = $pdf->GetX();

        $cell_width = 25; $cell_height=14; 
        $j = 20;  // This value will be coming from Database so we dont know how many pages the report is going to be 
        for ($i = 0; $i<$j ; $i++){
            $pdf->MultiCell($cell_width,$cell_height,'Hello1',1);
            $current_x+=$cell_width;
            $pdf->Ln();
        }

        $pdf->Output();


        ?>

Note : The $j value will be coming from the database so we don't know how many pages is the report going to be.

推荐答案

To add an A4 page, with portrait orientation, do:

$pdf->AddPage("P","A4");

Create a new class which extends the FPDF class, and override the pre-defined Footer method.

Example:

class PDF extends FPDF
{
    function Footer()
    {
        // Go to 1.5 cm from bottom
        $this->SetY(-15);
        // Select Arial italic 8
        $this->SetFont('Arial','I',8);
        // Print centered page number
        $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
    }
}

其他推荐答案

According to my comment you can place

$pdf->PageNo();

on your page where ever you like. Also you can add a placeholder to this

$pdf->AliasNbPages(); 

What would look like

$pdf->AliasNbPages('{totalPages}');

By default it's {nb}. It's not necessary to add a placeholder

Than you could add the pagesum like

$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);

or without your own placeholder

$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{nb}", 0, 1);

this would produce e.g.

Page 1/10

in case there were 10 pages :)

But beware

Using the placeholder will mess up the width of the cell. So if you have e.g. 180 page-width than 90 isn't the mid anymore (In the line where you use the placeholder). You will see if you try :)

相关标签/搜索