为什么Dompdf无法在laravel 5.2中加载我的视图?

时间:2016-08-10 15:57:16

标签: php html laravel-5.2 dompdf

我第一次使用laravel工作,我需要创建一个PDF,我读到了Dompdf,我设置了它,如果我把html代码放在: loadHtml();但是我无法加载视图,这是例外:

  

未定义变量:invoice(查看:C:\ Users \ Einar \ Documents \ PROYECTOS \ Ecotoner \ Actual \ EcoToner \ resources \ views \ ordenes \ invoice.blade.php)

我关注此代码:

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

这是我的观点:

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

希望有人可以帮助我。感谢

1 个答案:

答案 0 :(得分:0)

根据文档laravel-dompdf,尝试将代码更改为以下

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