Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

otachan's avatar

DOMPDF download pdf on different users (dynamically)

I have this one page which requires an id like posts/show/{id} and how do i do it in dompdf

this is the controller

public function pdf($id){
    $post = Post::find($id);
    $pdf = PDF::loadView('posts/show');
    return $pdf->stream('invoice.pdf');

    }

it is showing undefined variable posts (View: resources\views\posts\show.blade.php)

i have tried this

$pdf = PDF::loadView('posts/show', $post);

but it says that array_merge(): Argument #2 is not an array

0 likes
2 replies
Dirk313's avatar

For anyone looking at a solution you could do something like this, (Im also still new so code can be made cleaner)

So first in your controller you could do this

      public function viewpdf(Employee $employee) {

      $employee = Employee::find($employee); //Fnd it by variable name

    view()->share('employees', $employee);
    $employee = Pdf::loadView('employee.viewpdf')
        ->setPaper('a4', 'lanscape')
        ->setWarnings(false)
        ->setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']);
        return $employee->stream();

and in your index page you could add a link like this.

<a class="btn btn-primary" href="{{ URL::to('employee/viewpdf', ['employee' => $employee])}}">Export to 
PDF </a>

and in your routes you could use the route like this,

Route::get('/employee/viewpdf/{employee}', [EmployeesController::class, 'viewpdf'])->name('employee/viewpdf');

Like i said there could be better ways of doing it but it works ; )

rizaldieqy's avatar

@Dirk313 Hi, i want to ask something about your code.

in view()->share('employees', $employee);

what's the meaning of 'employees' ? Is it naming of blade or something??

Please or to participate in this conversation.