Ahsan Ahsan -3 years ago 459
PHP Question

Laravel dompdf error "Image not found or type unknown"

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download