0

Hello for those who are using barryvdh\dompdf laravel. I have a problem with the image of the PDF that I am sending in email. My issue is that the image background would not show and it would say that Image not found or type unknown. It just occurs this week as in the past it is working fine. I test in my local and the image background is showing while in the server it is not showing.

epermit.blade.php

<img class="mx-auto" src="{{ asset('/images/permit.jpg') }}"> 

In my controller

$pdf = PDF::setOptions(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true]) ->loadView('epermitpdf', compact('permit', 'student')); Storage::put('public/pdf/epermit.pdf', $pdf->output()); 

in mailable laravel

return $this->markdown('permit-markdown')->attach(Storage::disk('public')->path('pdf/epermit.pdf')); 

I try exchanging another pathname in src such as <img class="mx-auto" src="www.domain.com\permit"> as I try to put in a server to have easy access. It works for a while but after it would say again Image not found or type unknown.

5
  • you are saying on local it works but not on server, does both are using the same environment, I read from answer below you are using Win on local but are you using Win on server as well Commented Jan 25, 2021 at 8:53
  • Yes exactly sir. I used windows server and xampp as the server same with local Commented Jan 25, 2021 at 9:00
  • I have found one possible error, you have not mentioned disk when you insert using put, Storage::disk('local')->put('public/pdf/epermit.pdf', $pdf->output()); Commented Jan 25, 2021 at 9:16
  • you can change local to public as well Commented Jan 25, 2021 at 9:19
  • okay sir i'll try that one. I hope it will be okay Commented Jan 26, 2021 at 1:28

3 Answers 3

0

Try changing this line

Storage::put('public/pdf/epermit.pdf', $pdf->output());

to this

Storage::put(public_path('pdf/epermit.pdf'), $pdf->output());

then check if the file exists using your browser at

www.your-website.com/pdf/epermit.pdf

Sign up to request clarification or add additional context in comments.

5 Comments

Impossible to create the root directory
You need to double check the permissions on your public/pdf/ folder. You can try this, go to the main laravel folder via command line and type this (chown -R www-data:root .) or (chown -R www-data:www-data .)
chown would not work. I'm using windows and xampp
Ok on windows you should not need it. Can you paste in here the full path to your public/pdf folder including c:\full-path\
create a folder here - c:\xampp\htdocs\proj-name\public\pdf and try run the script again
0

I struggled a lot with this as well , tried SetOptions etc. And that's what solved the issue for me:

In the controller:

return PDF::loadView('your.view')->stream(); 

In the view:

<img src="{{public_path().'/storage/images/pdf/your_image.jpeg'}}"> 

Comments

0

To display images in DOMPDF, do not use public_path() or asset().
Instead, use the full domain from .env (APP_URL) so the image path is always resolved correctly.

# .env APP_URL=https://your-domain.com # config/app.php 'url' => env('APP_URL', 'https://your-domain.com'), # Controller $imgLogo = config('app.url') . '/img_folder/your_image.png'; # Blade <img src="{{ $imgLogo }}" width="200"> 

This way, DOMPDF only renders the HTML, and images are loaded directly from the full URL.

3 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
I have edit, thanks
I have edited my answer for clarity, added proper examples for using public_path() and full URLs, and included tips on permissions and caching.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.