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

KHAN's avatar
Level 4

laravel-dompdf - Download PDF On Click

Hey,

I have an event resource and a page which lists all the attendees. I have a requirement where the admin would like to print to pdf of all the attendees.

Controller:

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\EventModel;
use App\User;
use App\Mailers\AppMailer;
use Barryvdh\DomPDF\PDF;

    public function index($id)
    {

        $event = EventModel::findOrFail($id);
        $ids = $event->rsvps()->pluck('user_id');
        $attendees = User::whereIn('id', $ids)->get();

        return view('admin.events.attendees.index')->with('attendees', $attendees)
            ->with('event', $event);
    }


    public function printToPDF($eventId)
    {
        $event = EventModel::findOrFail($eventId);
        $ids = $event->rsvps()->pluck('user_id');
        $attendees = User::whereIn('id', $ids)->get();

        $pdf = PDF::loadView('admin.events.attendees.index')->with('attendees', $attendees)->with('event', $event);
        return $pdf->download('event-attendees.pdf');
    }

View:

@extends('admin.layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">

                        <h4>Attendee List</h4>

                <div class="row">
                    <div class="col-sm-2 pull-right">
                        <a class="btn btn-primary btn-block">Send Email</a>
                    </div>
                    <div class="col-sm-2 pull-right">
                        <a href="{{ route('admin-event-attendees-print', $event->id) }}"  class="btn btn-primary btn-block">Print To PDF</a>
                    </div>
                </div>
                <br>


                <div class="table-responsive">
                    <table class="table table-striped table-hover table-condensed">
                        <thead>
                        <tr>
                            <th></th>
                            <th class="text-center">#</th>
                            <th class="text-center">Title</th>
                            <th class="text-center">First Name</th>
                            <th class="text-center">Last Name</th>
                            <th class="text-center">Email</th>
                            <th class="text-center">Speciality</th>
                            <th class="text-center">Reject</th>
                        </tr>
                        </thead>
                        <tbody>

                        @foreach($attendees as $index => $attendee)
                            <tr class="text-center">
                                <td><input name="{{$attendee->id}}" type="checkbox" value=""></td>
                                <td>{{$index+1}}</td>
                                <td>{{$attendee->title}}</td>
                                <td>{{$attendee->firstname}}</td>
                                <td>{{$attendee->lastname}}</td>
                                <td>{{$attendee->email}}</td>
                                <td>{{$attendee->speciality()->first()->name }}</td>
                                <td><a href="{{route('admin-event-attendees-reject',[$event->id, $attendee->id ])}}"
                                       class="btn btn-warning">
                                        <i class="fa fa-trash"></i>
                                    </a>
                                </td>


                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                </div>

            </div>
        </div>
    </div>
@endsection

Route

    Route::get('/events/{id}/attendees', ['as' => 'admin-event-attendees', 'uses' => 'Admin\EventAttendeesController@index']);
    Route::get('/events/{id}/attendees/print', ['as' => 'admin-event-attendees-print', 'uses' => 'Admin\EventAttendeesController@printToPDF']);

Im having trouble getting to work. First im getting an error

Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context

Secondly how do i properly pass in variables when loading the pdf view.

thanks

0 likes
6 replies
KHAN's avatar
Level 4

@tomi

yeah i just saw that actually.

But now im getting another error.

ErrorException in AdobeFontMetrics.php line 45:
fopen(/var/www/html/projectnamehere/storage/fonts//31f9bb221845230a28b1181b1825b8b9.ufm): failed to open stream: No such file or directory

Please or to participate in this conversation.