Dompdf Laravel error "Image not found or type unknown "

45 Views Asked by At

i cannot download pdf file with my image, iam using dompdf in my laravel project.

here is my controller code :

public function downloadPdf()
    {
        $cakes = Cake::all();

        $data = [
            'cakes' => $cakes,
        ];

        $pdf = PDF::loadView('pages.cake.pdf', $data)
            ->setOptions(['defaultFont' => 'sans-serif'])
            ->setPaper('landscape', 'a4');

        return $pdf->download('kue.pdf');
    }

and here is my view download :

<table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Nama Kue</th>
                                <th>Harga</th>
                                <th>Foto</th>
                                <th>Dibuat Oleh</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($cakes as $item)
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td class="align-middle">{{ $item->nama }}</td>
                                    <td class="align-middle">{{ $item->harga }}</td>
                                    <td>
                                        **<img src="{{ asset('fotoKue/kue-fazril.jpg') }}" alt="Foto Kue"
                                            style="heigth: 8rem; width: 8rem;">**
                                    </td>
                                    <td class="align-middle p-0">
                                        {{ $item->creator_name . ' - ' . $item->creator_nim }}
                                    </td>
                            @endforeach
                        </tbody>
                    </table>

iam using src into my public directory for sampling.

i want to my problem dompdf cannot download pdf with imgae, is solved.

1

There are 1 best solutions below

0
CersKrisjanis On

You are using src="{{ asset('fotoKue/kue-fazril.jpg') }}". For web views it is the way to go.

It creates url like http://localhost/assets/fotoKue/kue-fazril.jpg. Problem here is that DOMPDF needs file relative path on server. In most cases it won't(on docker always) work with localhost, because it is not available.

Try src="{{ public_path('assets/fotoKue/kue-fazril.jpg') }}". This will create /var/www/html/public/assets/fotoKue/kue-fazril.jpg