TCPDF save page number in mysql

qa test
qa test used Ask the Experts™
on
I am using tcpdf 6.2.13 to print mysql data into pdf format. I am creating custom index with school name and page number. For that i have fetched the mysql data in foreach loop and loading school id to store page number in mysql for that school.

Issue is using TCPDF to get the page number i am trying following code $pdf->getAliasNumPage() but when i try to store it in variable it shows empty.

Output: Day School{:pnp:}

If i remove exit and generate the pdf then it shows page number in pdf file like
Output: Day School 2

why $pdf->getAliasNumPage() showing {:pnp:} and not showing page number?
why it's value can not be stored in to variable or database?

Can anyone help me to get the page number in tcpdf to store in database? these function $pdf->getAliasNumPage() shows page number for pdf but when i pass it's value in mysql query or in variable it shows empty why?

Any help would be appreciated. Code:

   $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
   $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('School DB');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
     $PgNo= $pdf->getAliasNumPage();
    $pdf->SetFont('dejavusans', '', 10);
    $rlt = $this->db->query("SELECT `school_pageno`,`school_country_id`,`school_state_id`,`school_city_id` FROM `school` where `school_country_id` = 2 group by `school_state_id`  order by `school_state_id` asc");
     $schoolquery = $rlt->result_array();
       foreach ($schoolquery as $key => $statevalue)
     {
     $html .= "<br/>". $statevalue['name'];
      $pnom =  $pdf->getAliasNumPage();
       $this->db->where('school_id', $statevalue['id']);
        $this->db->update('school', array('school_pageno' => $pnom));
      }
    //print_r($html);exit;
   $pdf->writeHTML($html, true, false, true, false, '');

Page number value doesn't work it shows {:pnp:} if i enable print_r.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
Try TCPDF::PageNo()
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
I am not sure what the question is
{:pnp:}

Is the correct output from that function based on the source.

This is a placeholder for the page number that is populated when the page is rendered out - I don't think it does what you think it does (want it to do)

I suspect you want $pdf->PageNo()
Most Valuable Expert 2011
Top Expert 2016

Commented:
@Julian: Are you able to get to the online docs for TCPDF?  I tried to find the man page reference for the PageNo() method, and it looks like the TCPDF online docs may have been hacked.  Formatting was all bollixed up, links didn't work consistently, stray HTML, etc.  If you can find a working link to the PageNo() method, it might be helpful.  It returns the current page number as an integer, and tests out correctly in my examples.

This is the kind of thing I'm seeing:
https://tcpdf.org/docs/source_docs/functions_func_p/#index_p
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
@Julian: Are you able to get to the online docs for TCPDF?
No just get an error not found - had to browse the code to see how things worked.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Yeah, too bad about the docs.  It's an open source project; maybe someone will come to the rescue!
Brian TaoSenior Business Solutions Consultant
Top Expert 2014
Commented:
@Ray and @Julian: The online doc was working well until this early morning.  I guess the author just updated it today, and messed up the links.
Those broken links can be "fixed" by changing the path "classTCPDF" to all lowercase and adding a "/" there.
from bad link:
https://tcpdf.org/docs/source_docs/classTCPDF#aa6e5b824889f7c484cc76ce1c134e2ed
to correct one:
https://tcpdf.org/docs/source_docs/classtcpdf/#aa6e5b824889f7c484cc76ce1c134e2ed
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Here is where I went
https://tcpdf.org/docs/
This page has an extra option at the top that is not visible in the links you provide. Link: Docs
Click on that - all you get is a Google Custom Search - type PageNo into that and you get results

The first result points to
https://tcpdf.org/docs/source_docs/classTCPDF/
Link broken
Change the capitals to lower case on TCPDF and the link works

So yes the documentation is there - just you have to have worked for Microsoft in order to be able to find it.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Nice catch, Brian.  In my tests these methods gave equivalent output.
// BURP OUT THE PAGE NUMBER
var_dump($pdf->PageNo());
var_dump($pdf->getPage());

Open in new window

Author

Commented:
@ray, @julian: Thanks for you reply,  if i type  PageNO()  then it return 3 for all schools  which is not correct.
here is my code;

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
   $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('School DB');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
     $PgNo= $pdf->getAliasNumPage();
 $pdf->AddPage();
    $pdf->SetFont('dejavusans', '', 10);
    $rlt = $this->db->query("SELECT `school_pageno`,`school_country_id`,`school_state_id`,`school_city_id` FROM `school` where `school_country_id` = 2 group by `school_state_id`  order by `school_state_id` asc");
     $schoolquery = $rlt->result_array();
       foreach ($schoolquery as $key => $statevalue)
     {
     $html .= "<br/>". $statevalue['name'];
      $pnom =  $pdf->PageNo();
       $this->db->where('school_id', $statevalue['id']);
        $this->db->update('school', array('school_pageno' => $pnom));
      }
    //print_r($html);exit;
   $pdf->writeHTML($html, true, false, true, false, '');

As i entered PageNo(); it shows  following output:

school One 3
School two 3
School three 3

It's not showing correct page number.  i gone through all docs but failed to find a solution of it.
Most Valuable Expert 2011
Top Expert 2016
Commented:
This is not a TCPDF problem; if it were, it would be immediately obvious, well-known and already fixed.  It's probably a logic error in your scripts.  You might consider getting a professional programmer to help you design this application.  E-E has "gigs."  I'd like to help, but I don't have your database to test with, so all I can do is show you how to move from page to page in the PDF document with addPage().  In the code example above, the script calls addPage() once, then uses the value from pageNo() to update the database.  My guess is that there is more to the application than we can see here.

This shows how the addPage() and pageNo() methods work.
https://iconoun.com/demo/temp_qa_test.php
<?php // demo/temp_qa_test.php
/**
 * Demonstrate page numbers in a TCPDF document
 *
 * TCPDF 6.2.13
 *
 * See http://www.tcpdf.org/doc/code/annotated.html ?
 */
error_reporting(E_ALL);

// LOAD THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/tcpdf_config.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT SO WE CAN SUBSTITUTE OUR OWN METHODS
class PDF extends TCPDF { }

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);

// ADD A PAGE AND SHOW THE PAGE NUMBER
$pdf->AddPage('P', 'LETTER', TRUE);
var_dump($pdf->PageNo());

// ADD ANOTHER PAGE
$pdf->AddPage('P', 'LETTER', TRUE);
var_dump($pdf->PageNo());

// ADD YET ANOTHER PAGE
$pdf->AddPage('P', 'LETTER', TRUE);
var_dump($pdf->PageNo());

Open in new window

Outputs:
int(1)
int(2)
int(3)

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
another abandoned question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial