How to add page number in laravel dompdf?

6.4k views Asked by At

I add $pdf->set_option('isPhpEnabled', true); is like this :

public function listdata()
{
    $data = User::all();

    $pdf=PDF::loadView('print_tests.test_pdf', ['data' => $data]);
    $pdf->set_option('isPhpEnabled', true);
    $pdf->setPaper('L', 'landscape');
    return $pdf->stream('test_pdf.pdf');
}

In body (View) I add like this :

<script type="text/php">
    if ( isset($pdf) ) {
        // OLD 
        // $font = Font_Metrics::get_font("helvetica", "bold");
        // $pdf->page_text(72, 18, "{PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(255,0,0));
        // v.0.7.0 and greater
        $x = 72;
        $y = 18;
        $text = "{PAGE_NUM} of {PAGE_COUNT}";
        $font = $fontMetrics->get_font("helvetica", "bold");
        $size = 6;
        $color = array(255,0,0);
        $word_space = 0.0;  //  default
        $char_space = 0.0;  //  default
        $angle = 0.0;   //  default
        $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
    }
</script>

There is exist error like this :

FatalThrowableError in PrintTestController.php line 21: Call to undefined method Barryvdh\DomPDF\PDF::set_option()

Note :

I using barryvdh/laravel-dompdf": "^0.7.0" (v0.7)

Is there any people who can help to me?

1

There are 1 answers

6
prateekkathal On

Please use setOptions() instead of set_option()

public function listdata()
{
  $data = User::all();

  $pdf = PDF::loadView('print_tests.test_pdf', ['data' => $data]);
  $pdf->setOptions(['isPhpEnabled' => true]);
  $pdf->setPaper('L', 'landscape');
  return $pdf->stream('test_pdf.pdf');
}

and for page number,

  1. First of all please don't use something like <script type="text/php">...
  2. There is no function as such as page_text and nor is $pdf passed to the view, instead the data is.

The only way page number can be added is using page-breaks

<style>
.page-break {
    page-break-after: always;
}
</style>

Add that to your view and then by yourself add page numbers as text.