dompdf为页面编号,但跳过第一个

时间:2018-11-22 13:21:37

标签: php dompdf

我使用dompdf类,并且我需要为每个页面进行页面numembering

我做到了,而且有效:

$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();

$canvas = $dompdf->get_canvas();
$font = $dompdf->getFontMetrics()->get_font("helvetica", "bold");

$canvas->page_text(72, 18, "Header: {PAGE_NUM}", $font, 6, array(0,0,0));

我需要的只是跳过第一页,然后从第二页开始编号。

我该怎么做?

4 个答案:

答案 0 :(得分:1)

这对我有用:

$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();

$canvas = $dompdf->get_canvas();
$cpdf = $canvas->get_cpdf();

$font = $dompdf->getFontMetrics()->get_font("helvetica", "bold");

$firstPageId = $cpdf->getFirstPageId();
$objects = $cpdf->objects;
$pages = array_filter($objects, function($v) {
    return $v['t'] == 'page';
});
$number = 1;
foreach($pages as $pageId => $page) {
    if(($pageId + 1) !== $firstPageId) {
        $canvas->reopen_object($pageId + 1);
        $canvas->text(72, 18, "Header: $number", $font, 6, array(0,0,0));
        $canvas->close_object();
        $number++;
    }
}

$dompdf->stream();

这是如何工作的:

  1. https://github.com/dompdf/dompdf/blob/v0.8.2/lib/Cpdf.php#L32
  2. 获取所有页面
  3. 将文本添加到第一页以外的所有页面。

答案 1 :(得分:0)

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

$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开始,情况有所不同:

$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 :(得分:0)

以前没有使用过DOMPDF,但是在此GitHub DOMPDF Post中遇到了一个纯CSS解决方案,并准备了一个小例子,也许您会发现它很有用。

body {
  counter-reset: page;
}

.footer:not(:first-child) .page-number:before {
  counter-increment: page;
  color: red;
  content: 'Page ';
}

.footer:not(:first-child) .page-number:after {
  color: red;
  content: counter(page);
}
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 0</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 1</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 2</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 3</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 4</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 5</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 6</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 7</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 8</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 9</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 10</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 11</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 12</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 13</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 14</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>

答案 3 :(得分:0)

来自this link 您可以使用以下功能

// set page number on the left side
$pdf->page_text(16, $y, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, $color);