douping1993 2016-08-10 15:57
浏览 64
已采纳

为什么Dompdf无法在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\EcoToneresources\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

  • 写回答

1条回答 默认 最新

  • dongtuoji5396 2016-08-10 16:09
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号