dompdf Numbering pages but skip first one
Asked Answered
A

4

7

I use dompdf class and I need page numembering for every page

I did this and it works:

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

What I need is just skip first page and start numbering from second page.

How can I do this?

Abettor answered 22/11, 2018 at 13:21 Comment(0)
R
5

Here is what worked for me:

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

How this works:

  1. Get all the pages from https://github.com/dompdf/dompdf/blob/v0.8.2/lib/Cpdf.php#L32
  2. Add the text to all except the 1st page.
Riordan answered 2/12, 2018 at 16:6 Comment(4)
Thanks, it works, one side question please, how can change font size? I tried: $font = $dompdf->getFontMetrics()->get_font("helvetica", "24"); but this not worksAbettor
Try $canvas->text(72, 18, "Header: $number", $font, 24, array(0,0,0)); github.com/dompdf/dompdf/blob/…Riordan
- I will try tomorrow at work. Thank You very much !Abettor
Color me impressed.Photoemission
Z
1

Use a page script and skip if PageCount is 1 and then substract one to pageNumber and 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);
    }
});
Zygosis answered 15/8, 2023 at 13:12 Comment(0)
P
0

Have not worked with DOMPDF before but came across a pure CSS solution in this GitHub DOMPDF Post and prepared a small example, perhaps you will find it helpful.

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>
Plosive answered 30/11, 2018 at 19:43 Comment(0)
E
0

from this link you can use function like below

// set page number on the left side
$pdf->page_text(16, $y, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, $color);
Eggcup answered 1/12, 2018 at 12:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.