Laravel dompdf error "Image not found or type unknown"

Ahsan picture Ahsan · Aug 15, 2017 · Viewed 13.3k times · Source

I am getting error "Image not found or type unknown" after downloading PDF in Laravel 5.4 using dompdf package. Here is the method

public function pdf()
    {
        $users = User::get();
        $pdf = PDF::loadView('pdf', compact('users'));

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

My view file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PDF</title>
</head>
<body> 
    <div class="container">
        <div class="row">
            @foreach ($users as $user)
                <img src="public/storage/images/{{ $user->profile_pic }}" alt="" style="width: 150px; height: 150px;">
            @endforeach
        </div>
    </div>
</body>
</html>

If I try with static image name (like following), it works

<img src="public/storage/images/image_1.jpg" alt="" style="width: 150px; height: 150px;">

But does not work with dynamic name.

Please suggest how can I can fix it.

Answer

Alexej picture Alexej · Aug 15, 2017

According to this question you have to use the full server path try:

<img src="{{ public_path("storage/images/".$user->profile_pic) }}" alt="" style="width: 150px; height: 150px;">

Assuming the image is stored in your public directory.