dompdf 对页面进行编号,但跳过第一页

问题描述 投票:0回答:4

我使用

dompdf
类,我需要为每个页面添加页码

我这样做了并且有效:

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

我需要的是跳过第一页并从第二页开始编号。

我该怎么做?

php dompdf
4个回答
5
投票

这对我有用:

$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. 将文本添加到除第一页之外的所有页面。

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>


0
投票

来自此链接 你可以使用如下函数

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

0
投票

使用页面脚本,如果 PageCount 为 1,则跳过,然后将 pageNumber 和 pageCount 减一。

$canvas = $dompdf->getCanvas();
$canvas->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) {
    if($pageNumber>1){
    $number = $pageNumber - 1;
    $count = $pageCount -1;
    $text = "Page $number of $count";
    $font = $fontMetrics->getFont('Helvetica');
    $pageWidth = $canvas->get_width();
    $pageHeight = $canvas->get_height();
    $size = 10;
    $width = $fontMetrics->getTextWidth($text, $font, $size);
    $canvas->text($pageWidth - $width - 20, $pageHeight - 20, $text, $font, $size);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.