dompdf不在laravel工作

时间:2017-12-04 07:40:42

标签: php laravel dompdf

我已经创建了一个html视图页面,用于从此视图生成pdf报告,并且我还安装了dompdf库,我已经从此视图上的数据库表中加载了数据,并创建了一个超链接名称,如下载pdf。但当我点击此链接时,它显示未定义的变量 dsale 错误。

这是我的控制器方法。

 //pass data to view
public function dsreport($id,$fromdate,$todate)
{
    $dsale=DB::table('directsales')
            ->join('clients','directsales.client_id','=','clients.id')
            ->join('products','directsales.product_id','=','products.id')
            ->select('clients.client_name','clients.addr','directsales.*','products.name')
            ->where('directsales.client_id','=',$id)
            ->whereBetween('directsales.issue_date',[$fromdate,$todate])
            ->distinct()
            ->paginate(3);
            //->get();
        $sumt=DB::table('directsales')
            ->where('directsales.client_id','=',$id)
            ->sum('directsales.total');

    return view('reports.directsalereport',compact(['dsale','sumt']));
}

//generate pdf
public function dsalepdf()
{
    $pdf=PDF::loadView('reports.directsalereport');
    return $pdf->download('dsread.pdf');
}

这是我的观点部分。

<div class="panel-body">
    <center><h1><strong>M/s. Priti Enterprise</strong></h1></center>
    </center>
    <center><h2><i>Daily Sales Report</i></h2></center>
    <div class="row">
        <div class="col-md-4"><strong>Client Name: {{$dsale[0]->client_name}}</strong></div>
        <div class="col-md-4"></div>
        <div class="col-md-4"><strong>Delivered by: {{$dsale[0]->deliverd_by}}</strong></div>
    </div><br>
    <div class="row">
        <div class="col-md-4"><strong>Client address: {{$dsale[0]->addr}}</strong></div>
        <div class="col-md-4"></div>
        <div class="col-md-4"><strong>Date: {{$dsale[0]->issue_date}}</strong></div>
    </div><br><br>

    <table class="table table-bordered">
        <thead>
            <th class="col-md-1">SL no.</th>
            <th>Product Name</th>
            <th>Invoice no.</th>
            <th>Unit per ctn.</th>
            <th>Unit price</th>
            <th class="col-md-2">Sales</th>
            <th>Value</th>
        </thead>
        <tbody>
            @foreach($dsale as $d)
            <tr>
                <td>#</td>
                <td>{{$d->name}}</td>
                <td>{{$d->transaction_code}}</td>
                <td>{{$d->unitperctn}}</td>
                <td>{{$d->unitprice}}</td>
                <td>{{$d->ctn}} ctn {{$d->pcs}} pcs</td>
                <td>{{$d->total}}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <div class="col-md-11"></div>
    <div class="row"><strong>Total:{{$sumt}}</strong></div>
    <div>{{$dsale->links()}}</div>
    <div class="col-md-8"></div>
    <div class="row">
        <a href="{{route('downpdf')}}" class=" btn btn-info"><i class=" glyphicon glyphicon-ok"></i> Download PDF</a>
    </div>
</div>

2 个答案:

答案 0 :(得分:3)

compact中有一个数组。

您应该使用<div>{{$dsale[0]->links()}}</div>

我认为你不应该压缩数组。

return view('reports.directsalereport',compact('dsale','sumt'));

<强>编辑: 您也应该将数据传递到Pdf视图,因此下载pdf的方法应如下所示。

public function dsalepdf()
{

    $dsale=DB::table('directsales')
                ->join('clients','directsales.client_id','=','clients.id')
                ->join('products','directsales.product_id','=','products.id')
                ->select('clients.client_name','clients.addr','directsales.*','products.name')
                ->where('directsales.client_id','=',$id)
                ->whereBetween('directsales.issue_date',[$fromdate,$todate])
                ->distinct()
                ->paginate(3);

            $sumt=DB::table('directsales')
                ->where('directsales.client_id','=',$id)
                ->sum('directsales.total');

    $pdf=PDF::loadView('reports.directsalereport',['dsale' => $dsale,'sumt'=> $sumt]);
    return $pdf->download('dsread.pdf');
}

答案 1 :(得分:0)

您可能需要删除供应商文件夹,然后运行composer install