rafitio
rafitio

Reputation: 568

Passing variable from DOMPDF controller to view

I want create a DOMPDF with laravel, and I must passing my variable to view. I've been try passing variable like below, but it still not working yet.

here my Laravel Controller

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

the error on my view is :

Undefined variable: salesorder

How to passing some variable from Laravel controller to DOMPDF ?

PS : dd($data) result is correctly

Upvotes: 7

Views: 21256

Answers (5)

Sagar Sharma
Sagar Sharma

Reputation: 33

You can use library function to do that easily.

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

You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)

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

Upvotes: 3

ehsan
ehsan

Reputation: 121

I know this is old but this may help others. you need to use array key in your view.

Controller:

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

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

View:

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

Upvotes: 1

Rojith Peiris
Rojith Peiris

Reputation: 201

You may try this following

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

Upvotes: 4

Shrikant Systematix
Shrikant Systematix

Reputation: 21

You may try the following.

$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');

Upvotes: 2

Saumini Navaratnam
Saumini Navaratnam

Reputation: 8860

You have to pass the data as below

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

or try using compact

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

Upvotes: 5

Related Questions