5

Como faço para colocar o número de cada página com domPDF? Uso ele com o laravel.

Exemplo: Página 1-4, Página 2-4, Página 3-4 e Página 4-4;

Minha função para buscar os dados do banco:

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');
    }

E minha renderização:

<!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>

Agradeço a ajuda.

1 Resposta 1

3

Um exemplo básico:

<!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>

ou seja, $items é valor que eu envio e faço um laço de repetição das informações e aonde tem <script type='text/php'> é processado a paginação.

No controller adicione $pdf->getDOMPdf()->set_option('isPhpEnabled', true); para funcionar o código anterior, exemplo:

<?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
  • 2
    Bom dia Virgilio Novic. Cara, era isso mesmo que eu queria. Funcionou muito bem. Obrigado!!! 20/10/2018 às 13:17
  • Deram voto negativo, tem algum motivo?
    – novic
    13/04 às 23:27

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .