нумерация страниц dompdf, но пропустите первую - PullRequest
0 голосов
/ 22 ноября 2018

Я использую 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));

Мне нужно просто пропустить первую страницу и начать нумерациюсо второй страницы.

Как я могу это сделать?

Ответы [ 4 ]

0 голосов
/ 02 декабря 2018

Вот что сработало для меня:

$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. Добавить текст ввсе, кроме 1-й страницы.
0 голосов
/ 30 ноября 2018

Раньше не работал с DOMPDF, но наткнулся на чистое решение CSS в этом GitHub DOMPDF Post и подготовил небольшой пример, возможно, вы найдете его полезным.

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 голосов
/ 01 декабря 2018

из по этой ссылке Вы можете использовать функцию, как показано ниже

// set page number on the left side
$pdf->page_text(16, $y, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, $color);
0 голосов
/ 22 ноября 2018

попробуйте следующее в 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));
  }
');
...