简体   繁体   中英

How to show DOMPDF footer - Page number and Page count in all pages except last page

I am using DOMPDF to generate PDF file. I want footer(Page number and Page count) in all pages except last page. currently i am having this code

    $dompdf = new DOMPDF();
    $html = ob_get_contents();
    $dompdf->load_html($html);
    $dompdf->render();
    $canvas = $dompdf->get_canvas();
    $font = Font_Metrics::get_font("helvetica", "12");                  
    $canvas->page_text(500, 770, "Page {PAGE_NUM} - {PAGE_COUNT}", $font, 10, array(0,0,0));
    $canvas->page_text(260, 770, "Canny Pack", $font, 10, array(0,0,0));
    $canvas->page_text(43, 770, $date, $font, 10, array(0,0,0));
    $pdf = $dompdf->output();

the above code in showing footer in all pages. but i dont want footer in last page.

can anyone tell me how to do this?

The page_text() methods runs on all pages without restriction. If you use the text() method in combination with the page_script() method you can programmatically determine the pages on which you want to insert the text.

Something like the following should work:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
  if ($pdf->get_page_number() != $pdf->get_page_count()) {
    $font = Font_Metrics::get_font("helvetica", "12");                  
    $pdf->text(500, 770, "Page {PAGE_NUM} - {PAGE_COUNT}", $font, 10, array(0,0,0));
    $pdf->text(260, 770, "Canny Pack", $font, 10, array(0,0,0));
    $pdf->text(43, 770, $date, $font, 10, array(0,0,0));
  }
');
$pdf = $dompdf->output();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM