php FPDF 在每个 A4 尺寸页面的页脚处获取页码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23753991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FPDF Get page numbers at footer on Every A4 size page
提问by Jay
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.
我正在使用 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();
?>
Note : The $j value will be coming from the database so we don't know how many pages is the report going to be.
注意:$j 值将来自数据库,因此我们不知道报告将有多少页。
回答by Dwza
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
默认情况下它是 {nb}。没有必要添加占位符
Than you could add the pagesum like
比你可以添加 pagesum 像
$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
第 1/10 页
in case there were 10 pages :)
如果有 10 页 :)
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 :)
使用占位符会弄乱单元格的宽度。因此,如果您的页面宽度为 180,则 90 不再是中间(在您使用占位符的行中)。你会看到你是否尝试:)
回答by Vipin Kumar Soni
To add an A4 page, with portrait orientation, do:
要添加纵向的 A4 页面,请执行以下操作:
$pdf->AddPage("P","A4");
Create a new class which extends the FPDF
class, and override the pre-defined Footer
method.
创建一个扩展FPDF
类的新类,并覆盖预定义的Footer
方法。
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');
}
}