dongshandun4363 2017-04-11 11:49
浏览 66
已采纳

TCPDF在mysql中保存页码

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. please help!

  • 写回答

1条回答 默认 最新

  • douxiong2738 2017-04-11 17:42
    关注

    From the Documentation,

    https://tcpdf.org/docs/source_docs/classtcpdf/#ae11c5f52caa0df8609ec22c30cd54055

    Returns the string alias used for the page number. If the current font is unicode type, the returned string is surrounded by additional curly braces. This alias will be replaced by the page number.

    The reason for this is because at that point in the code, it doesn't know what the page number will end up being, because you have not written the html to pdf pages yet.

    If you were to write your html to the document throughout the loop, then you could use the PageNo() method to get the current page

    https://tcpdf.org/docs/source_docs/classtcpdf/#a9ad828b184f08828f570a7e52316ba79

    For example:

    foreach ($schoolquery as $key => $statevalue) 
    {
        // Get Page Number
        $pnom =  $pdf->PageNo();
        // Write information to document
        $html = "<br/>". $statevalue['name'];
        $pdf->writeHTML($html, true, false, true, false, '');
        // Store page information to database
        $this->db->where('school_id', $statevalue['id']);
        $this->db->update('school', array('school_pageno' => $pnom));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀