Page number with DOMPDF and LARAVEL

Asked

Viewed 985 times

4

How do I place the number of each page with domPDF? I use it with the Standard.

Example: Page 1-4, Page 2-4, Page 3-4 and Page 4-4;

My function to fetch database data:

public function print($id) {
        $budget = Budget::find($id);
        $budgetItems = BudgetItem::where('budget_id', '=', $budget->id)->get();
        $pdf = PDF::loadView('reports.budgetprint', ['budget' => $budget, 'budgetItems' => $budgetItems]);
        return $pdf->stream('budgetprint.pdf');
    }

And my rendering:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Report Quote</title>
</head>
<body>

    <div class="text-center">
      <img src="<?php echo public_path() . '/images/logoreport.png'; ?>" alt="Logo" width="300px">
    </div>

    <p>
      <span><strong>Client:</strong> {{ $budget->client_name }};</span>
      <span style="float: right;"><strong>Date:</strong> {{ date_format(new DateTime($budget->created_at), 'm-d-Y') }}</span>
    </p>

    <p>{{ $budget->client_address }}</p>
    <p>
      <span><strong>Cell:</strong> {{ $budget->client_phone }} / {{ $budget->client_cell_phone }}</span>
      <span style="float: right;"><strong>E-mail:</strong> {{ $budget->client_email }}</span>
    </p>

    <hr>

    <table class="table table-bordered table-condensed" style="margin-bottom: 100px;">
        <thead>
            <tr>
                <th style="width: 50px;">Room</th>
                <th>Items</th>
                <th style="width: 40px; text-align: center;">Qty</th>
                <th style="width: 80px; text-align: center;">Price</th>
                <th style="width: 80px; text-align: center;">Subtotal</th>
            </tr>
        </thead>
        <tbody>
          <tr>
              <td style="text-align: center;">-</td>
              <td>{{ $budgetItem->name }}</td>
              <td style="text-align: center;">{{ $budgetItem->amount }}</td>
              <td style="text-align: center;">{{ number_format($budgetItem->price, 2, '.', ',') }}</td>
              <td style="text-align: center;">{{ number_format($budgetItem->subtotal, 2, '.', ',') }}</td>
          </tr>
        </tbody>
    </table>
</body>
</html>

I appreciate the help.

1 answer

4


A basic example:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>           
    </head>
    <body>      
        @foreach($items as $item)
            <div>{{$item}}</div>
        @endforeach
        <script type='text/php'>
            if (isset($pdf)) 
            {               
                $pdf->page_text(60, $pdf->get_height() - 50, "{PAGE_NUM} de {PAGE_COUNT}", null, 12, array(0,0,0));
            }
        </script>
    </body>
</html>

ie, $items is value I send and make a loop of repetition of information and where has <script type='text/php'> is processed the pagination.

In the controller add $pdf->getDOMPdf()->set_option('isPhpEnabled', true); to run the previous code, example:

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ViewPdfController extends Controller
{
    public function index()
    {
        for ($i=0; $i < 100; $i++) 
        { 
            $items[$i] = $i + 1;
        }
        $pdf = \App::make('dompdf.wrapper');
        $pdf->loadView('dompdf', ['items' => $items]);  
        $pdf->getDOMPdf()->set_option('isPhpEnabled', true);            
        return $pdf->stream();
    }
}
  • 2

    Good morning Virgilio Novic. Man, that’s what I wanted. It worked great. Thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.