从DOMPDF控制器传递变量以查看

时间:2016-12-30 08:01:56

标签: laravel dompdf

我想用laravel创建一个DOMPDF,我必须将我的变量传递给view。我一直尝试传递如下变量,但它仍然无法正常工作。

这里是我的Laravel控制器

 public function pdf(Request $request, $id){
    $salesorder = $this->show($id)->salesorder;
    $detailservice = $this->show($id)->detailservice;
    $detailemployee = $this->show($id)->detailemployee;

    $data = [$salesorder, $detailemployee, $detailservice];

    $pdf = PDF::loadView('summary.invoice', $data);
    return $pdf->download('invoice.pdf');
}

我的观点错误是:

  

未定义的变量:salesorder

如何将一些变量从Laravel控制器传递给DOMPDF?

PS:dd($data)结果正确

5 个答案:

答案 0 :(得分:2)

您必须通过以下data

$data = [
    'salesorder'     => $salesorder,
    'detailemployee' => $detailemployee,
    'detailservice'  => $detailservice
];

或尝试使用compact

$data = compact('salesorder', 'detailemployee', 'detailservice');

答案 1 :(得分:2)

您可以使用库函数轻松地做到这一点。

$pdf_text = "It will be the text you want to load";
PDF::loadHTML($pdf_text)->save('public/you-file-name.pdf');

您可以更改方向和纸张大小,以及隐藏或显示错误(默认情况下,调试打开时会显示错误)

PDF::loadHTML($pdf_text)->setPaper('a4', 'landscape')->setWarnings(false)->save('public/you-file-name.pdf')

答案 2 :(得分:0)

您可以尝试以下操作。

$html = view('summary.invoice', ['salesorder' => $salesorder, 'detailemployee' => $detailemployee, 'detailservice' => $detailservice])->render();
$pdf = App::make('dompdf.wrapper');
$invPDF = $pdf->loadHTML($html);
return $pdf->download('invoice.pdf');

答案 3 :(得分:0)

您可以尝试以下操作

public function pdf(Request $request, $id){
    $salesorder = $this->show($id)->salesorder;
    $detailservice = $this->show($id)->detailservice;
    $detailemployee = $this->show($id)->detailemployee;

    $pdf = PDF::loadView('summary.invoice', array('salesorder' => $salesorder,'detailemployee'=>$detailemployee,'detailservice'=>$detailservice));
    return $pdf->download('invoice.pdf');
}

答案 4 :(得分:0)

我知道这很旧,但这可能对其他人有帮助。您需要在视图中使用数组键。

控制器:

$data = [
     'foo'    => $bar,
];

$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('doc.pdf');

查看:

<p>{{$foo}}</p>