dounayan3643 2016-02-18 20:04 采纳率: 0%
浏览 64
已采纳

使用{PAGE_NUM}为页面编号时,make dompdf跳过第一页

I have the following code below that perfectly adds the page number to each of my pages in the bottom right corner of the document.

I have a header page that DOES NOT need a page number, so would like to skip the number for it.

Is there a way of doing this? or at the very least modifying the code to be page_num+1, page_count-1, and then div over the header page so that it doesnt show?

$dompdf->render();
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("helvetica", "bold");
$canvas->page_text(522, 770, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 10, array(0,0,0));
  • 写回答

3条回答 默认 最新

  • dongliang2058 2016-02-19 18:12
    关注

    You can't do this using the page_text() method since this method applies the specified text on all pages. What you would want to use instead is the page_script() method which gives you capabilities similar to dompdf's embedded script, run on all pages.

    Since you only need to subtract out the first page you can just subtract one from the current page and page total to get the correct page numbering.

    Try the following in dompdf 0.6.2 or earlier:

    $dompdf->render();
    $canvas = $dompdf->get_canvas();
    $canvas->page_script('
      if ($PAGE_NUM > 1) {
        $font = Font_Metrics::get_font("helvetica", "bold");
        $current_page = $PAGE_NUM-1;
        $total_pages = $PAGE_COUNT-1;
        $pdf->text(522, 770, "Page: $current_page of $total_pages", $font, 10, array(0,0,0));
      }
    ');
    

    Things are a bit different starting with dompdf 0.7.0:

    $dompdf->render();
    $canvas = $dompdf->getCanvas();
    $canvas->page_script('
      if ($PAGE_NUM > 1) {
        $font = $fontMetrics->getFont("helvetica", "bold");
        $current_page = $PAGE_NUM-1;
        $total_pages = $PAGE_COUNT-1;
        $pdf->text(522, 770, "Page: $current_page of $total_pages", $font, 10, array(0,0,0));
      }
    ');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题