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 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决