在 TCPDF 中,如何将当前页码获取为整数?
In TCPDF, how to get current page number as integer?
我尝试使用 $pdf-getAliasNumPage() ,如果以 PDF 格式呈现,它会显示页码。
但是当我在基本 PHP 上进行实验并打印出来时,它 returns 只有“{:pnp:}”。
如果页面已经更改,我曾经有一个 if 条件,以便我可以再次重置值 = 0,但由于 getAliasNumPage() 的返回值等于“{:即插即用:}".
有什么方法可以找到整数形式的页码吗?那是什么TCPDF功能?
我只声明了一次 AddPage(),因为我不需要它。
已经使用了$pdf->getPage();只有 returns 0.
谢谢!
$this->payroll_id = $request->getParameter('payroll_id');
$this->class = new PsPayroll();
$config = sfTCPDFPluginConfigHandler::loadConfig();
$pdf = new reportPDF(LANDSCAPE, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$month = $this->class->mlFetchPayroll();
$month->execute(array($this->payroll_id));
$catch = "";
while ($print_month = $month->fetch()){
$catch = $print_month[8];
}
$pdf->SetCreator(Aaron);
$pdf->setMonth($catch);
$pdf->SetAuthor('');
$pdf->SetTitle('Payroll Report');
$pdf->SetSubject('');
$pdf->SetKeywords('');
$pdf->SetHeaderData('logo.png', '25', '', $catch);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP + 4, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER + 15);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setFontSubsetting(true);
$pdf->SetFont('freesans', '', 9.5, '', true);
$html .= '<table class = "table hover">
<thead>
<tr>
<th>Employee Information</th>
<th>Monthly Salary</th>
<th>Earnings</th>
<th>Deductions</th>
<th>Net Pay Signature</th>
</tr>
</thead>
<tbody>';
$pager = 1;
$prepare = $this->class->mlFetchPayroll();
$prepare->execute(array($this->payroll_id));
while ($myrow = $prepare->fetch()){
$total_net_pay = 0;
$total_earning = 0;
$total_deduction = 0;
$html .= '<br><tr nobr="true"><td>' . $myrow[2] . '<br>'. $myrow[3] . '<br>' . $myrow[4] . '</td><td>' . number_format($myrow[5], 2) . '</td><td>';
$earning_array = $this->class->mlFetchEarningPayslip($myrow[10]);
foreach ($earning_array as $k => $valk){
$net_pay += $valk;
$total_earning += $valk;
$html .= '<table><tr><td>' . $k . '</td><td>' . number_format($valk, 2) . '</td></tr></table>';
}
$html .= "</td><td>";
$deduction_array = $this->class->mlFetchDeductionPayslip($myrow[10]);
foreach ($deduction_array as $i => $val){
$net_pay -= $val;
$total_deduction += $val;
$html .= '<table><tr><td>' . $i . '</td><td>'. number_format($val, 2) . '</td></tr></table>';
}
$html .= '</td><td>';
$array_date = $this->class->mlDivideMonth($myrow[8]);
$k = 0;
foreach ($array_date as $value){
$breakdown = 0.00;
$k++;
$monthly_salary = $myrow[5];
$monthly_salary += $total_earning - $this->class->mlFetchPERA($myrow[10]);
$monthly_salary -= $total_deduction;
$break_down = $monthly_salary/ 4;
$html .= '<table><tr><td align="right">';
if ($breakdown < 0)
$html .= 0;
else if ($k == 2){
$a = round($break_down, 2);
$b = $a + $this->class->mlFetchPERA($myrow[10]);
$c = number_format($b, 2);
$html .= $c;
}
else{
$a = round($break_down, 2);
$b = number_format($a, 2);
$html .= $b;
}
$html .= ' </td><td>............... </td><td>' . $value . '</td></tr></table>';
}
$html .= '</td></tr>';
$pager = $pdf->getAliasNumPage();
if ($pager == 1){
$html .= 'This is first page ' . $pdf->getAliasNumPage();
}
else{
$pager = $pdf->getAliasNumPage();
$html .= "This is page " . $pdf->getAliasNumPage();
}
// $html .= 'sample '. $pager;
}
$html .= '</tbody></table>';
$pdf->AddPage();
$pdf->writeHTML($html, true, false, false, true, 'Aaron');
$pdf->Output('payroll_'.$catch.'.pdf', 'I');
throw new sfStopException();
正确的函数是 PageNo()
http://www.tcpdf.org/doc/code/classTCPDF.html#a9ad828b184f08828f570a7e52316ba79
TCPDF::PageNo()
Returns the current page number.
Returns
int page number
编辑。
好的,现在(我想)我明白你想做什么了,你只添加 1 页并使用自动分页符,你也不想在页脚中自动编号。在这种情况下,您应该使用 getAliasNumPage() 和 getAliasNbPages()。定义以下变量(随意编辑文本):
$PgNo= "This is the page " . $pdf->getAliasNumPage() . " of " . $pdf->getAliasNbPages();
将它放在 php 文档中的任何位置 (在定义字体后放置它很重要) 然后只需使用变量 $PgNo(或者你将调用它)在任何你需要的地方。您只需定义一次,稍后它将根据它位于 pdf 文档中的哪个页面自动获取值。
PageNo()
和 getPage()
都将 return 当前页码。
$this->PageNo();
$this->getPage();
$this->getAliasNumPage() 的结果不是数字。这是... {:pnp:}
你可以检查这个:
$text = "My Footer - Página ".$this->getAliasNumPage().'/'.$this->getAliasNbPages();
$this->Cell(0, 10, $text, 0, false, 'C', 0, '', 0, false, 'T', 'M');
你会看到号码。
但是如果你执行
echo $text;
你会看到字符串没有数字,只有 {:pnp:}
这是因为此时(所以在执行Footer函数的时候),PDF还没有创建。所以 TCPDF 无法知道页数。所以它不能给你这个值。
你怎么办?为了在某些页面上获得不同的页脚,例如,您必须计算页码。因此,在 Footer() 函数的开头声明一个全局 $var。在 Footer() 函数之前,将 $var 设置为 0,然后在 Footer() 内部仅 $var++.
对于最后一页,只需使用:
class mypdf extends tcpdf
{
// Declare for use in all function
protected $last_page = false;
// Will be called at the end
public function Close()
{
$this->last_page = true;
parent::Close();
}
public function Footer()
{
if ($this->last_page)
{
// Here you can displat the footer for last page
}
}
}
编辑:我忘记了一个选项。如果您想知道显示前的完整页数,或者(例如)如果您想知道 PDF 块的大小以查看它是否适合页面等等,您可以使用 rollbackTransaction();
首先执行所有操作(AddPages、MultiCell 等),这将使您能够读取结果的大小和所有其他值。
然后执行 $pdf = $pdf -> rollbackTransaction();这将 "undo" 一切,然后你 运行 反对你的所有功能,这次使用你从 "first run".
获得的所有价值
我尝试使用 $pdf-getAliasNumPage() ,如果以 PDF 格式呈现,它会显示页码。
但是当我在基本 PHP 上进行实验并打印出来时,它 returns 只有“{:pnp:}”。
如果页面已经更改,我曾经有一个 if 条件,以便我可以再次重置值 = 0,但由于 getAliasNumPage() 的返回值等于“{:即插即用:}".
有什么方法可以找到整数形式的页码吗?那是什么TCPDF功能?
我只声明了一次 AddPage(),因为我不需要它。
已经使用了$pdf->getPage();只有 returns 0.
谢谢!
$this->payroll_id = $request->getParameter('payroll_id');
$this->class = new PsPayroll();
$config = sfTCPDFPluginConfigHandler::loadConfig();
$pdf = new reportPDF(LANDSCAPE, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$month = $this->class->mlFetchPayroll();
$month->execute(array($this->payroll_id));
$catch = "";
while ($print_month = $month->fetch()){
$catch = $print_month[8];
}
$pdf->SetCreator(Aaron);
$pdf->setMonth($catch);
$pdf->SetAuthor('');
$pdf->SetTitle('Payroll Report');
$pdf->SetSubject('');
$pdf->SetKeywords('');
$pdf->SetHeaderData('logo.png', '25', '', $catch);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP + 4, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER + 15);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setFontSubsetting(true);
$pdf->SetFont('freesans', '', 9.5, '', true);
$html .= '<table class = "table hover">
<thead>
<tr>
<th>Employee Information</th>
<th>Monthly Salary</th>
<th>Earnings</th>
<th>Deductions</th>
<th>Net Pay Signature</th>
</tr>
</thead>
<tbody>';
$pager = 1;
$prepare = $this->class->mlFetchPayroll();
$prepare->execute(array($this->payroll_id));
while ($myrow = $prepare->fetch()){
$total_net_pay = 0;
$total_earning = 0;
$total_deduction = 0;
$html .= '<br><tr nobr="true"><td>' . $myrow[2] . '<br>'. $myrow[3] . '<br>' . $myrow[4] . '</td><td>' . number_format($myrow[5], 2) . '</td><td>';
$earning_array = $this->class->mlFetchEarningPayslip($myrow[10]);
foreach ($earning_array as $k => $valk){
$net_pay += $valk;
$total_earning += $valk;
$html .= '<table><tr><td>' . $k . '</td><td>' . number_format($valk, 2) . '</td></tr></table>';
}
$html .= "</td><td>";
$deduction_array = $this->class->mlFetchDeductionPayslip($myrow[10]);
foreach ($deduction_array as $i => $val){
$net_pay -= $val;
$total_deduction += $val;
$html .= '<table><tr><td>' . $i . '</td><td>'. number_format($val, 2) . '</td></tr></table>';
}
$html .= '</td><td>';
$array_date = $this->class->mlDivideMonth($myrow[8]);
$k = 0;
foreach ($array_date as $value){
$breakdown = 0.00;
$k++;
$monthly_salary = $myrow[5];
$monthly_salary += $total_earning - $this->class->mlFetchPERA($myrow[10]);
$monthly_salary -= $total_deduction;
$break_down = $monthly_salary/ 4;
$html .= '<table><tr><td align="right">';
if ($breakdown < 0)
$html .= 0;
else if ($k == 2){
$a = round($break_down, 2);
$b = $a + $this->class->mlFetchPERA($myrow[10]);
$c = number_format($b, 2);
$html .= $c;
}
else{
$a = round($break_down, 2);
$b = number_format($a, 2);
$html .= $b;
}
$html .= ' </td><td>............... </td><td>' . $value . '</td></tr></table>';
}
$html .= '</td></tr>';
$pager = $pdf->getAliasNumPage();
if ($pager == 1){
$html .= 'This is first page ' . $pdf->getAliasNumPage();
}
else{
$pager = $pdf->getAliasNumPage();
$html .= "This is page " . $pdf->getAliasNumPage();
}
// $html .= 'sample '. $pager;
}
$html .= '</tbody></table>';
$pdf->AddPage();
$pdf->writeHTML($html, true, false, false, true, 'Aaron');
$pdf->Output('payroll_'.$catch.'.pdf', 'I');
throw new sfStopException();
正确的函数是 PageNo()
http://www.tcpdf.org/doc/code/classTCPDF.html#a9ad828b184f08828f570a7e52316ba79
TCPDF::PageNo()
Returns the current page number.
Returns
int page number
编辑。
好的,现在(我想)我明白你想做什么了,你只添加 1 页并使用自动分页符,你也不想在页脚中自动编号。在这种情况下,您应该使用 getAliasNumPage() 和 getAliasNbPages()。定义以下变量(随意编辑文本):
$PgNo= "This is the page " . $pdf->getAliasNumPage() . " of " . $pdf->getAliasNbPages();
将它放在 php 文档中的任何位置 (在定义字体后放置它很重要) 然后只需使用变量 $PgNo(或者你将调用它)在任何你需要的地方。您只需定义一次,稍后它将根据它位于 pdf 文档中的哪个页面自动获取值。
PageNo()
和 getPage()
都将 return 当前页码。
$this->PageNo();
$this->getPage();
$this->getAliasNumPage() 的结果不是数字。这是... {:pnp:} 你可以检查这个:
$text = "My Footer - Página ".$this->getAliasNumPage().'/'.$this->getAliasNbPages();
$this->Cell(0, 10, $text, 0, false, 'C', 0, '', 0, false, 'T', 'M');
你会看到号码。
但是如果你执行
echo $text;
你会看到字符串没有数字,只有 {:pnp:}
这是因为此时(所以在执行Footer函数的时候),PDF还没有创建。所以 TCPDF 无法知道页数。所以它不能给你这个值。
你怎么办?为了在某些页面上获得不同的页脚,例如,您必须计算页码。因此,在 Footer() 函数的开头声明一个全局 $var。在 Footer() 函数之前,将 $var 设置为 0,然后在 Footer() 内部仅 $var++.
对于最后一页,只需使用:
class mypdf extends tcpdf
{
// Declare for use in all function
protected $last_page = false;
// Will be called at the end
public function Close()
{
$this->last_page = true;
parent::Close();
}
public function Footer()
{
if ($this->last_page)
{
// Here you can displat the footer for last page
}
}
}
编辑:我忘记了一个选项。如果您想知道显示前的完整页数,或者(例如)如果您想知道 PDF 块的大小以查看它是否适合页面等等,您可以使用 rollbackTransaction(); 首先执行所有操作(AddPages、MultiCell 等),这将使您能够读取结果的大小和所有其他值。 然后执行 $pdf = $pdf -> rollbackTransaction();这将 "undo" 一切,然后你 运行 反对你的所有功能,这次使用你从 "first run".
获得的所有价值