9

I get from here : https://github.com/barryvdh/laravel-dompdf

My controller is like this :

public function listdata()
{
    $pdf=PDF::loadView('print_tests.test_pdf');
    $pdf->setPaper('L', 'landscape');
    return $pdf->stream('test_pdf.pdf');
}

My view is like this :

<table>
    <tr>
        <th>header1</th>
        <th>header2</th>
        <th>header3</th>
    </tr>
    <tr>
        <td>content-row-1</td>
        <td>content-row-1</td>
        <td>content-row-1</td>
    </tr>
    <tr>
        <td>content-row-2</td>
        <td>content-row-2</td>
        <td>content-row-2</td>
    </tr>
</table>

I want every page there is a page number

Is there any people who can help me?

samuel toh
  • 6,836
  • 21
  • 71
  • 108
  • Have you seen this? The accepted answer can be found here. http://stackoverflow.com/questions/19983610/how-to-get-page-number-on-dompdf-pdf-when-using-view – Bryan Kneis Dec 22 '16 at 10:34

3 Answers3

9

Try this one.

<style>
   .pagenum:before {
        content: counter(page);
    }
</style>
<body>

    <span class="pagenum"></span>

    <table>
        <tr>
          <th>header1</th>
          <th>header2</th>
          <th>header3</th>
     </tr>
     <tr>
         <td>content-row-1</td>
          <td>content-row-1</td>
         <td>content-row-1</td>
     </tr>
     <tr>
        <td>content-row-2</td>
        <td>content-row-2</td>
        <td>content-row-2</td>
     </tr>
    </table>
</body>
larp
  • 1,017
  • 1
  • 14
  • 23
7

you can do like that for example..

in your controller :

$pdf = app('dompdf.wrapper');
$pdf->getDomPDF()->set_option("enable_php", true);
$data = ['title' => 'Testing Page Number In Body'];
$pdf->loadView('welcomeView', $data);

in your welcomeView.blade.php

<html>
 <body>
   <h1> {{ $title }} </h1>

   <script type="text/php">
    if (isset($pdf)) {
        $x = 250;
        $y = 10;
        $text = "Page {PAGE_NUM} of {PAGE_COUNT}";
        $font = null;
        $size = 14;
        $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>

</body>
</html>
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
6

Follow below steps to achieve it:
- Enable DOMPDF_ENABLE_PHP from /config/dompdf.php
- Publish vendor file via php artisan vendor:publish command
- Pass $pdf object from controller:
- Add below code inside the view file:

<script type="text/php">
    if ( isset($pdf) ) {
        $font = Font_Metrics::get_font("helvetica", "bold");
        $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
    }
</script> 

You can get more idea from Page count and page number

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57