Php 如何使用fpdf在分页符上重复页眉
我正在尝试使用FPDF创建报告。下面的代码可以很好地生成报告,但现在我需要在每一页上重复表头部分。另外,如何在页眉和页脚上添加边距。目前只有徽标部分重复。如何使用公司名称徽标和表格标题重复整个部分 这是我的表格标题部分:Php 如何使用fpdf在分页符上重复页眉,php,html,css,fpdf,Php,Html,Css,Fpdf,我正在尝试使用FPDF创建报告。下面的代码可以很好地生成报告,但现在我需要在每一页上重复表头部分。另外,如何在页眉和页脚上添加边距。目前只有徽标部分重复。如何使用公司名称徽标和表格标题重复整个部分 这是我的表格标题部分: $pdf->SetFillColor(170, 170, 170); //gray $pdf->setFont("Arial","B","9"); $pdf->setXY(10, 40); $pdf->Cell(25,
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
<?php
$localhost = 'localhost'; //name of server. Usually localhost
$database = 'payorder'; //database name.
$username = 'root'; //database username.
$password = ''; //database password.
// connect to db
$conn = mysql_connect($localhost, $username, $password) or die('Error connecting to mysql');
$db = mysql_select_db($database,$conn) or die('Unable to select database!');
require('lib/include/fpdf/fpdf.php');
class PDF extends FPDF {
//Page header
function Header(){
//Logo
$this->Image('images/logo.jpg',15,8,20);
}
//Page footer
function Footer(){
//Position at 1.5 cm from bottom
$this->SetY(-15);
//$this->SetX(-35);
//Arial italic 8
$this->SetFont('Arial','I',8);
//Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
$pdf = new PDF();
//$pdf->open();
$pdf->AddPage("P","A4"); // P =portrait L = Landscape
$pdf->AliasNbPages(); // necessary for x of y page numbers to appear in document
$pdf->SetAutoPageBreak(true, 20);
// document properties
$pdf->SetAuthor('Sonali Bank Limited');
$pdf->SetTitle('Daily Transuction Report');
$pdf->SetFont('Arial','B',14);
$pdf->Cell(30,10,' Company name Here');
// Add date report ran
$pdf->SetFont('Arial','I',10);
$date = date("d/m/Y");
$pdf->Cell(20,20,"Company Subtitle here Dated: ".$date);
$pdf->SetDrawColor(0, 0, 0); //black
//table header
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
$y = 50;
$x = 10;
$pdf->setXY($x, $y);
$pdf->setFont("Arial","","9");
//payorder data query
$sql = "SELECT * FROM payorder_data WHERE po_date ='$report_day'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_num_rows($res);
while($row = mysql_fetch_array($res)){
$pdf->Cell(25, 8, $row['po_date'], 1);
$pdf->Cell(35, 8, $row['po_number'], 1);
$pdf->Cell(50, 8, $row['po_ben_name'], 1);
$pdf->Cell(30, 8, $row['contra_date'], 1);
$pdf->Cell(30, 8, $row['po_amount'], 1);
$y += 8;
if ($y > 260) // When you need a page break
{
$pdf->AddPage();
$y = 40;
}
$pdf->setXY($x, $y);
}
$pdf->Output();
}
这是我的完整代码:
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
<?php
$localhost = 'localhost'; //name of server. Usually localhost
$database = 'payorder'; //database name.
$username = 'root'; //database username.
$password = ''; //database password.
// connect to db
$conn = mysql_connect($localhost, $username, $password) or die('Error connecting to mysql');
$db = mysql_select_db($database,$conn) or die('Unable to select database!');
require('lib/include/fpdf/fpdf.php');
class PDF extends FPDF {
//Page header
function Header(){
//Logo
$this->Image('images/logo.jpg',15,8,20);
}
//Page footer
function Footer(){
//Position at 1.5 cm from bottom
$this->SetY(-15);
//$this->SetX(-35);
//Arial italic 8
$this->SetFont('Arial','I',8);
//Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
$pdf = new PDF();
//$pdf->open();
$pdf->AddPage("P","A4"); // P =portrait L = Landscape
$pdf->AliasNbPages(); // necessary for x of y page numbers to appear in document
$pdf->SetAutoPageBreak(true, 20);
// document properties
$pdf->SetAuthor('Sonali Bank Limited');
$pdf->SetTitle('Daily Transuction Report');
$pdf->SetFont('Arial','B',14);
$pdf->Cell(30,10,' Company name Here');
// Add date report ran
$pdf->SetFont('Arial','I',10);
$date = date("d/m/Y");
$pdf->Cell(20,20,"Company Subtitle here Dated: ".$date);
$pdf->SetDrawColor(0, 0, 0); //black
//table header
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
$y = 50;
$x = 10;
$pdf->setXY($x, $y);
$pdf->setFont("Arial","","9");
//payorder data query
$sql = "SELECT * FROM payorder_data WHERE po_date ='$report_day'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_num_rows($res);
while($row = mysql_fetch_array($res)){
$pdf->Cell(25, 8, $row['po_date'], 1);
$pdf->Cell(35, 8, $row['po_number'], 1);
$pdf->Cell(50, 8, $row['po_ben_name'], 1);
$pdf->Cell(30, 8, $row['contra_date'], 1);
$pdf->Cell(30, 8, $row['po_amount'], 1);
$y += 8;
if ($y > 260) // When you need a page break
{
$pdf->AddPage();
$y = 40;
}
$pdf->setXY($x, $y);
}
$pdf->Output();
}
您应该修改此部分的代码
if ($y > 260) // When you need a page break
{
$pdf->AddPage();
$y = 40;
}
做这样的东西
if ($y > 260) // When you need a page break
{
$pdf->AddPage();
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
$y = 50;
$x = 10;
$pdf->setXY($x, $y);
$pdf->setFont("Arial","","9");
}
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
编辑:
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
<?php
$localhost = 'localhost'; //name of server. Usually localhost
$database = 'payorder'; //database name.
$username = 'root'; //database username.
$password = ''; //database password.
// connect to db
$conn = mysql_connect($localhost, $username, $password) or die('Error connecting to mysql');
$db = mysql_select_db($database,$conn) or die('Unable to select database!');
require('lib/include/fpdf/fpdf.php');
class PDF extends FPDF {
//Page header
function Header(){
//Logo
$this->Image('images/logo.jpg',15,8,20);
}
//Page footer
function Footer(){
//Position at 1.5 cm from bottom
$this->SetY(-15);
//$this->SetX(-35);
//Arial italic 8
$this->SetFont('Arial','I',8);
//Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
$pdf = new PDF();
//$pdf->open();
$pdf->AddPage("P","A4"); // P =portrait L = Landscape
$pdf->AliasNbPages(); // necessary for x of y page numbers to appear in document
$pdf->SetAutoPageBreak(true, 20);
// document properties
$pdf->SetAuthor('Sonali Bank Limited');
$pdf->SetTitle('Daily Transuction Report');
$pdf->SetFont('Arial','B',14);
$pdf->Cell(30,10,' Company name Here');
// Add date report ran
$pdf->SetFont('Arial','I',10);
$date = date("d/m/Y");
$pdf->Cell(20,20,"Company Subtitle here Dated: ".$date);
$pdf->SetDrawColor(0, 0, 0); //black
//table header
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
$y = 50;
$x = 10;
$pdf->setXY($x, $y);
$pdf->setFont("Arial","","9");
//payorder data query
$sql = "SELECT * FROM payorder_data WHERE po_date ='$report_day'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_num_rows($res);
while($row = mysql_fetch_array($res)){
$pdf->Cell(25, 8, $row['po_date'], 1);
$pdf->Cell(35, 8, $row['po_number'], 1);
$pdf->Cell(50, 8, $row['po_ben_name'], 1);
$pdf->Cell(30, 8, $row['contra_date'], 1);
$pdf->Cell(30, 8, $row['po_amount'], 1);
$y += 8;
if ($y > 260) // When you need a page break
{
$pdf->AddPage();
$y = 40;
}
$pdf->setXY($x, $y);
}
$pdf->Output();
}
要设置边距,您可以查看以下文档:
在本文档中,不包括底边距,因此有关底边距,请访问以下帖子:
您也可以这样设置边距
if ($y > 260) // When you need a page break
{
$pdf->AddPage();
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","B","9");
$pdf->setXY(10, 40);
$pdf->Cell(25, 10, "Payorder Date", 1, 0, "L", 1);
$pdf->Cell(35, 10, "Payorder Number", 1, 0, "L", 1);
$pdf->Cell(50, 10, "Beneficiary Name", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Contra Date", 1, 0, "L", 1);
$pdf->Cell(30, 10, "Amount", 1, 0, "L", 1);
$y = 50;
$x = 10;
$pdf->setXY($x, $y);
$pdf->setFont("Arial","","9");
}
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
您能否以数组或json的形式提供数据集,以便我们test@ARIFMAHMUDRANA谢谢Arif先生这是sqlquestion代码更新谢谢你的回答。我试试这个