dompdf page_script() variables

2020-07-27 20:42发布

问题:

I'm trying to generate a PDF with multiple sets of page numberings and with unnumbered pages as well. I will not be able to tell in advance how long each set of pages is as they are dynamically generated. For example, the PDF may contain 10 total pages where pages 1-4 have "Page X of 4" in the footer, page 5 is unnumbered, pages 6-8 have "Page X of 3", and pages 9-10 are unnumbered.

Right now I have page numberings implemented using the page_script() and text() functions. Essentially, what I think I need, is a way to be able to pass variable from the document to the page_script() function as the PDF is generated. This would allow me to add something like <?php $page_count = false; ?> or <?php $page_count_reset = true; ?> at different places in the document and act accordingly in the page_script() function. As far as I can tell, this doesn't seem possible.

I am able to set globals within the document <?php $GLOBALS['page_count'] = false; ?> and read them from within page_script() BUT these are processed all at once. So whatever I set the last $GLOBALS['page_count'] to in the document is what $GLOBALS['page_count'] is in the entire page_script() function.

I hope this makes some kind of sense. My next step is to generate multiple PDFs and join them together but that's something I don't want to do unless I have to. Does anyone have any thought?

回答1:

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



标签: dompdf