9

I tried using $pdf-getAliasNumPage() and it shows the page number if it is rendered in PDF.

But when I experimented on a basic PHP and printed it, it returns only "{:pnp:}".

I used to have an if condition if the page is already changed so that I can reset a value = 0 again, but the condition was always false due to the returned value of getAliasNumPage() is equal to "{:pnp:}".

Is there any way I can find the page number as an integer? What TCPDF function is that?

I only declared AddPage() once because I don't need it.

Already used $pdf->getPage(); only returns 0.

Thanks!

$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();
passerby
  • 163
  • 2
  • 2
  • 8
  • I have faced same problem. https://stackoverflow.com/questions/51761325/in-tcpdf-version-6-this-getaliasnbpages-return-value-ptp-so-how-can-i-get-i – Bhavin Thummar Aug 09 '18 at 09:25

3 Answers3

10

both PageNo() and getPage() will return the current page number.

$this->PageNo();

$this->getPage();
twostripe
  • 101
  • 1
  • 4
  • 1
    I have same issue i have got the {:ptp:} from function getAliasNbPages in TCPDF version 6. But this function return integer value in TCPDF version 5. You can check this link. https://stackoverflow.com/questions/51761325/in-tcpdf-version-6-this-getaliasnbpages-return-value-ptp-so-how-can-i-get-i – Bhavin Thummar Aug 09 '18 at 09:23
5

The correct function is PageNo()

http://www.tcpdf.org/doc/code/classTCPDF.html#a9ad828b184f08828f570a7e52316ba79

TCPDF::PageNo()

Returns the current page number.

Returns

int page number

Edit.

OK, now (I think) I understand what you want to do, you add only 1 page and use auto page breaks, you also don't want to automatically number the pages in the footer. In that case you should use getAliasNumPage() and getAliasNbPages(). Define the following variable (edit the text as you will):

$PgNo= "This is the page " . $pdf->getAliasNumPage() . " of " . $pdf->getAliasNbPages();

Put it anywhere in the php document (it is important to put it after defining the fonts) and then just use the variable $PgNo (or however you will call it) wherever you need. You have to define it only once and it will later on get the values automatically depending on which page in the pdf document it is located.

Community
  • 1
  • 1
  • Yeah I already tried that, I just declared AddPage() once because I have no need to declare another one, since it will mess up with my report's logic. It just returns 0. – passerby Apr 22 '15 at 04:27
  • Then there has to be some error in your code. Can you update your question and post the code? Edit. It is really strange because I just check it and on the first page it returns 1, so where does your 0 come from? You didn't edit any of the TCPDF's files? –  Apr 22 '15 at 04:34
  • There was none unfortunately, I'll show you the sample report. Nope I didn't edit something in the classes. I just used a single AddPage(). – passerby Apr 22 '15 at 05:41
  • Ok, now you have to specify a little bit which part of it gives you the problem and where do you use the PageNo(). Also: from what I understand PDF is generated correctly, but you would like to get the page number as php value? Could you elaborate on that? You mean like, you want that value without generating the pdf? I'm confused. –  Apr 23 '15 at 20:41
  • No I want to get the page value without using any footer, since AddPage() only occurs once. – passerby Apr 24 '15 at 01:28
  • Thanks! But when I get the value of getAliasNumPage() it only returns {{:pnp:}}? I need to get the integer value? – passerby Apr 24 '15 at 06:08
  • By get the value you mean it is printed on the pdf or you don't get to produce the pdf at all? Long shot but can you try using dejavusans instead of freesans? If you trying to get that value without producing the pdf (for example echoing it from php) it will not work (obviously) and will return {:pnp:} which is a placeholder. –  Apr 24 '15 at 09:46
  • I have faced same problem please check this question https://stackoverflow.com/questions/51761325/in-tcpdf-version-6-this-getaliasnbpages-return-value-ptp-so-how-can-i-get-i – Bhavin Thummar Aug 09 '18 at 09:24
2

The result of $this->getAliasNumPage() is NOT a number . It's... {:pnp:} You can check this:

 $text = "My Footer - Página ".$this->getAliasNumPage().'/'.$this->getAliasNbPages();
 $this->Cell(0, 10, $text, 0, false, 'C', 0, '', 0, false, 'T', 'M');

And you will see the number.

But if you perform

  echo $text;

you'll see the string doesn't have the number but only {:pnp:}

This came from the fact at this time (so when it executes the Footer function), the PDF is not yet created. So TCPDF can't know the number of pages. So it can't give you this value.

What can you do? in order to get a different footer eg on some pages, you must count the page number. So, declare a global $var at beginning of the Footer() function. Before the Footer() function, set $var to 0 and then, inside Footer() just $var++.

For the last page, just use this:

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 display the footer for the last page
         } 
     }
  }   

Edit: I forgot one option. If you want to know the full number of pages before the display or (eg) if you want to know the size of a PDF bloc to see if it fits a page and so on, you can use the rollbackTransaction(); First, you perform all operations (AddPages, MultiCell, and so on) which will give you the capability of the reading size of the result and all other values. Then you perform a $pdf = $pdf -> rollbackTransaction(); which will "undo" everything and then you run all your functions again, using this time all the value you get from the "first run".

Community
  • 1
  • 1
Peter
  • 1,247
  • 19
  • 33
  • Thanks @peter your solution helped me to add custom signature html block at the bottom of pdf's last page :) – Kamlesh Mar 27 '22 at 17:08