首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在TCPDF中,如何获取当前的整型页码?

在TCPDF中,如何获取当前的整型页码?
EN

Stack Overflow用户
提问于 2015-04-22 11:38:30
回答 2查看 21.1K关注 0票数 7

我尝试使用$ PDF -getAliasNumPage(),如果以PDF格式呈现,它会显示页码。

但是当我在一个基本的PHP上进行实验并打印它时,它只返回"{:pnp:}“。

我曾经有一个if条件,如果页面已经改变,那么我可以再次重置一个值=0,但是这个条件总是假的,因为getAliasNumPage()的返回值等于"{:pnp:}“。

有没有办法找到一个整数形式的页码?TCPDF函数是什么?

我只声明了AddPage()一次,因为我不需要它。

已使用$pdf->getPage();仅返回0。

谢谢!

代码语言:javascript
复制
$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 &nbsp;&nbsp;&nbsp;&nbsp; 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 .= '&nbsp;&nbsp;&nbsp;</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();
EN

回答 2

Stack Overflow用户

发布于 2018-06-24 00:44:29

PageNo()getPage()都将返回当前页码。

代码语言:javascript
复制
$this->PageNo();

$this->getPage();
票数 8
EN

Stack Overflow用户

发布于 2019-05-08 20:06:41

$this->getAliasNumPage()的结果不是数字。这是..。{:pnp:}您可以检查以下内容:

代码语言:javascript
复制
 $text = "My Footer - Página ".$this->getAliasNumPage().'/'.$this->getAliasNbPages();
 $this->Cell(0, 10, $text, 0, false, 'C', 0, '', 0, false, 'T', 'M');

你会看到这个数字。

但如果你表演

代码语言:javascript
复制
  echo $text;

您将看到字符串没有数字,只有{:pnp:}

这是因为此时(所以当它执行Footer函数时),PDF还没有被创建。因此,TCPDF不能知道页数。所以它不能给你这个值。

你能怎么做?为了得到不同的页脚,你必须计算页码。因此,在$var ()函数的开头声明一个全局Footer。在Footer()函数之前,将$var设置为0,然后在Footer()内部只设置$var++。

对于最后一页,只需使用以下内容:

代码语言:javascript
复制
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等等),这将使你能够读取结果的大小和所有OHER值。然后,执行$pdf = $pdf -> rollbackTransaction();,它将“撤消”所有内容,然后对所有函数运行,使用这次从“第一次运行”中获得的所有值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29787283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档