dompdf page_script() variables

jcon

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?

BrianS

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to render DOMPDF within a page

From Dev

DomPDF {PAGE_NUM} not on first page

From Dev

How to use php variable inside pdf ->page_script()?

From Dev

Make an image in dompdf cover all of the first page

From Dev

dompdf inserts blank page at end of document

From Dev

Dompdf default page header content overlap

From Dev

Adding cover image with page number in dompdf

From Dev

dompdf timeout when printing 25 page pdf

From Dev

DOMPDF - break page when not fit content

From Dev

DOMPDF page orientation both landscape and portrait

From Dev

dompdf freezes not showing content. page is blank

From Dev

dompdf, Store the Contents of a Wordpress Page in a Variable

From Dev

dompdf timeout when printing 25 page pdf

From Dev

dompdf - set page margin only after first page

From Dev

Laravel 5 domPDF PAGE_NUM and PAGE_COUNT

From Dev

how to get page count in dompdf without using page text

From Dev

Class 'Dompdf\Dompdf' not found

From Dev

dompdf HTML to PDF - can't set margin of page

From Dev

Dompdf: how to get background image to show on first page only

From Dev

How to get page number on dompdf PDF when using "view"

From Dev

dompdf: top-margin after page break not working

From Dev

DOMPDF, Table row height back to normal in second page

From Dev

dompdf page-break-inside: avoid on table not working

From Dev

DOMPDF Installation

From Dev

Multi-page pdf using barryvdh dompdf Laravel-4 and loadView rather than loadHtml

From Dev

DOMPDF error: Class Dompdf\FrameDecorator\AbstractFrameDecorator not fund

From Dev

CSS not working with DOMPDF

From Dev

DomPDF: Image not readable or empty

From Dev

DOMPDF displaying pictures, but wrong

Related Related

HotTag

Archive