dompdf page_script() variables

爷,独闯天下 提交于 2019-12-07 15:53:26

You're on the right track. Almost there, actually. What you need to do is track your sections in your global variable. The following snippets can be placed in your HTML for use with dompdf 0.6.1.

1) Set up some global variables first thing in the body:

$GLOBALS['start_pages'] = array( );
$GLOBALS['current_start_page'] = null;
$GLOBALS['show_page_numbers'] = false;

2) At the start of each section fill out the start_pages global:

$GLOBALS['current_start_page'] = $pdf->get_page_number();
$GLOBALS['start_pages'][$pdf->get_page_number()] = array(
  'show_page_numbers' => true,
  'page_count' => 1
);

3) At the end of each section, record the number of pages:

$GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;

4) Use page script to write out your page numbering for each section:

$pdf->page_script('
  if ($pdf) {
    if (array_key_exists($PAGE_NUM, $GLOBALS["start_pages"])) {
      $GLOBALS["current_start_page"] = $PAGE_NUM;
      $GLOBALS["show_page_numbers"] = $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["show_page_numbers"];
    }
    if ($GLOBALS["show_page_numbers"]) {
      $font = Font_Metrics::get_font("helvetica", "bold");
      $pdf->text(10, 10, "Page " . ($PAGE_NUM - $GLOBALS["current_start_page"] + 1) . " of " . $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["page_count"], $font, 12);
    }
  }
');

The final document would look something like this:

<html>
<body>

<script type="text/php">
  // setup
  $GLOBALS['start_pages'] = array( );
  $GLOBALS['current_start_page'] = null;
  $GLOBALS['show_page_numbers'] = false;
</script>

<script type="text/php">
  // section start
  $GLOBALS['current_start_page'] = $pdf->get_page_number();
  $GLOBALS['start_pages'][$pdf->get_page_number()] = array(
    'show_page_numbers' => true,
    'page_count' => 1
  );
</script>

<p>lorem ipsum ... <!-- lots of text -->

<script type="text/php">
  // record total number of pages for the section
  $GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;
</script>
<div style="page-break-before: always;"></div>
<script type="text/php">
  // section start
  $GLOBALS['current_start_page'] = $pdf->get_page_number();
  $GLOBALS['start_pages'][$pdf->get_page_number()] = array(
    'show_page_numbers' => false,
    'page_count' => 1
  );
</script>

<p>lorem ipsum ... <!-- lots of text -->

<script type="text/php">
  // record total number of pages for the section
  $GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;
</script>

<script type="text/php">
    $pdf->page_script('
      if ($pdf) {
        if (array_key_exists($PAGE_NUM, $GLOBALS["start_pages"])) {
          $GLOBALS["current_start_page"] = $PAGE_NUM;
          $GLOBALS["show_page_numbers"] = $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["show_page_numbers"];
        }
        if ($GLOBALS["show_page_numbers"]) {
          $font = Font_Metrics::get_font("helvetica", "bold");
          $pdf->text(10, 10, "Page " . ($PAGE_NUM - $GLOBALS["current_start_page"] + 1) . " of " . $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["page_count"], $font, 12);
        }
      }
    ');
</script>

</body>
</html>

You can see a sample of this in practice here: http://eclecticgeek.com/dompdf/debug.php?identifier=e980df4efacf5202c2f1d31579f09c56

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!