Skip to content
Advertisement

Laravel DOMPDF: how to load image from storage folder?

An image stored in the /storage/app/ folder of Laravel 5.2 isn’t being rendered into DOMPDF. I receive an error saying: image not found or type unknown.

The image’s path is storage/app/v6_dashboard.png and I tried these:

<img src="/media/v6_dashboard.png" />
<img src="media/v6_dashboard.png" />
<img src="http://localhost:8000/media/v6_dashboard.png" />

which use this route:

Route::get('/media/{img}', function ($img) {
  $image = Image::make(Storage::disk('local')->get($img));
  return Response::make($image->encode('jpg'), 200, ['Content-Type' => 'image/jpeg']);
});

Note that I have this config set: "DOMPDF_ENABLE_REMOTE" => true

It works in a typical HTML view but not in DOMPDF. Is there a fundamental incompatibility or am I doing something wrong?

Advertisement

Answer

In the end, I bypassed the route and used a str_replace to switch /media/ to the absolute path like so:

str_replace('/media/', '../storage/app/', $link);

Slightly counter-intuitive to go from public to storage in the HTML but it seems to be the only way for DOMPDF to render the image. Also found out that DOMPDF won’t render remote images over HTTPS.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement