Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMPDF with laravel - cannot load images in html file

I'm trying to generate pdf file with laravel and dompdf lib. Here is my code from controller:

public function create(Request $request) {
    $pdf = new Dompdf();
    $html = Storage::disk('local')->get('public/pdf/template.html');
    $pdf->loadHtml($html, 'UTF-8');
    $pdf->setPaper('A4', 'portrait');
    $pdf->render();
    $filename = "Hi!";
    return $pdf->stream($filename, ["Attachment" => false]);
}

And in template.html I got some html, somewhere there I got images like this:

<img src="img/logo.png" alt="BTS">

But dompdf cant write this image: Image not found or type unknown. My file system is:

.pdf ...img ......here is images ...template.html ...*some_fonts_files*

But at the same time, fonts are loading fine. What should I do to render images in template.html?

like image 283
Alexxosipov Avatar asked Oct 21 '25 13:10

Alexxosipov


2 Answers

I believe that for dompdf you need to pass a properly accessable url rather than relative paths.

Try <img src="{{ asset('img/logo.png')}}" alt="BTS">

Alternatively if asset doesn't work please try <img src="{{ public_path('img/logo.png')}}" alt="BTS">

Also please note that asset uses the public directory as the base_url to calculate the 'asset' from.

like image 70
Simon R Avatar answered Oct 24 '25 03:10

Simon R


Me too i struggled for the same error for 3 days. Also when i tried tricks mentioned above, none of them worked too. But when i changed the image file to .jpg file(extension) everything worked well.

<img src="{{ public_path('img/logo.jpg') }}" >
  • Laravel version: 5.8.38
  • PHP version: 7.3
  • barryvdh/laravel-dompdf version: 0.8.6
like image 30
Angelwise Avatar answered Oct 24 '25 03:10

Angelwise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!