Search code examples
phphtmllaravel-5dompdf

Why the Dompdf can't load my view in laravel 5.2?


I'm using laravel for my work for first time, and I need to create a PDF, I read about Dompdf, I setted up and it works fine if I put the html code in the: loadHtml(); but I can't load a view, this is the exception:

Undefined variable: invoice (View: C:\Users\Einar\Documents\PROYECTOS\Ecotoner\Actual\EcoToner\resources\views\ordenes\invoice.blade.php)

I'm following this code:

use Illuminate\Http\Request;

use App\Http\Requests;

class PdfController extends Controller
{
//
    public function invoice() 
    {
      $data = $this->getData();
      $date = date('Y-m-d');
      $invoice = "2222";
      $view =
\View::make('ordenes.invoice',compact('data','date','invoice'));
      $pdf = \App::make('dompdf.wrapper');
      $pdf->loadHTML($view);
      return $pdf->stream('invoice');

      //$pdf = \PDF::loadView('ordenes.invoice', $data);
      //return $pdf->stream();
  }

  public function getData() 
  {
    $data =  [
        'quantity'      => '1' ,
        'description'   => 'some ramdom text',
        'price'   => '500',
        'total'     => '500'
    ];
    return $data;
  }
}

and this is my view:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Example 2</title>
<!--{!! Html::style('assets/css/pdf.css') !!}-->
</head>
<body>

<main>
  <div id="details" class="clearfix">
    <div id="">
      <h1>INVOICE</h1>
      <div class="">Date of Invoice:</div>
    </div>
  </div>
  <table border="0" cellspacing="0" cellpadding="0">
    <thead>
      <tr>
        <th class="no">#</th>
        <th class="desc">DESCRIPTION</th>
        <th class="unit">UNIT PRICE</th>
        <th class="total">TOTAL</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="no">{{ $data['quantity'] }}</td>
        <td class="desc">{{ $data['description'] }}</td>
        <td class="unit">{{ $data['price'] }}</td>
        <td class="total">{{ $data['total'] }} </td>
      </tr>

    </tbody>
    <tfoot>
      <tr>
        <td colspan="2"></td>
        <td >TOTAL</td>
        <td>$6,500.00</td>
      </tr>
    </tfoot>
  </table>
</body>
</html>

Hope someone can help me. Thanks


Solution

  • As per documentation laravel-dompdf try changing your code to following

    $pdf = \PDF::loadView('ordenes.invoice',compact('data','date','invoice'));
    return $pdf->download('invoice.pdf');