-->

DOMPDF(0.8.3): Skip the 1st page having header

2019-08-31 02:26发布

问题:

I am using dompdf version 0.8.3. We added some header in each page but we want the header will only appear on the 2nd page onwards.

What we have right now is we have header and footer on each page but we want to display the 'P O #: number ?> when the page is 2nd page onwards.

Controller

<!-- My other function/data -->
$pdf = PDF::loadView('purchase-order.export.export-pdf', $data)->setPaper('a4');
$pdf->getDomPDF()->set_option("enable_php", true);
return $pdf->stream('purchase-order-'.$purchase_order->number.'.pdf');

PDF

<style type="text/css">
    @page {
            margin-top: 40px !important;
            margin-bottom: 50px !important;
        }
</style>
<div class="page-break">
    <div id="page-wrap">
        <script type="text/php">
            if (isset($pdf)) {
                $x = 25;
                $y = 800;
                $text = "page {PAGE_NUM} of {PAGE_COUNT}";
                $page_num = "{PAGE_NUM}";
                $text2 = "This is a system generated document";
                $font = 'Arial';
                $size = 11;
                $color = array(0.333,0.333,0.333);
                $word_space = 0.0;  //  default
                $char_space = 0.0;  //  default
                $angle = 0.0;   //  default
                // header
                $pdf->page_text(500,10,'P O #: <?= $purchase_order->number ?>',$font,$size,$color,$word_space,$char_space,$angle);
                // footer
                $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
                // generated
                $pdf->page_text(410, $y, $text2, $font, $size, $color, $word_space, $char_space, $angle);
            }
        </script>
    </div>
</div>

Question: Is it possible to skip the 1st page to have the header?

回答1:

If I got your question right, I believe you want to skip first page and display your header, footer and generated text on second page, and so on. You can add condition to check what's the current page and then display header pagination only if that page is greater than 1:

if ($page_num > 1) {
  // header
  $pdf->page_text(500,10,'P O #: <?= $purchase_order->number ?>',$font,$size,$color,$word_space,$char_space,$angle);
  // footer
  $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
  // generated
  $pdf->page_text(410, $y, $text2, $font, $size, $color, $word_space, $char_space, $angle);
}