Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In TCPDF, how to get current page number as integer?

Tags:

php

pdf

tcpdf

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();
like image 872
passerby Avatar asked Apr 22 '15 03:04

passerby


1 Answers

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

$this->PageNo();

$this->getPage();
like image 64
twostripe Avatar answered Sep 19 '22 23:09

twostripe