使用{PAGE_NUM}为页面编号时,使dompdf跳过第一页 [英] make dompdf skip first page when numbering pages with {PAGE_NUM}

查看:96
本文介绍了使用{PAGE_NUM}为页面编号时,使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.

有没有办法做到这一点?还是至少将代码修改为page_num + 1,page_count-1,然后在页眉页上div使其不显示?

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));


推荐答案

您不能使用 page_text()方法,因为此方法将指定的文本应用于所有页面。相反,您想使用的是 page_script()方法,该方法具有类似于dompdf的嵌入式脚本的功能,可在所有页面上运行。

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.

在dompdf 0.6.2或更早版本中尝试以下操作:

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));
  }
');

从dompdf 0.7.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));
  }
');

这篇关于使用{PAGE_NUM}为页面编号时,使dompdf跳过第一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆